-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (56 loc) · 1.85 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import express, { Request, Response, NextFunction } from "express";
import { config } from "dotenv";
config();
import fs from "node:fs";
import ffmpeg from "fluent-ffmpeg";
import { createServer } from "node:http";
import uploadRoutes from "./routes/upload.routes.ts";
import videoRoutes from "./routes/video.routes.ts";
import streamRoutes from "./routes/stream.routes.ts";
import morgan from "morgan";
import { getBucket } from "./mongoUploader/connect2mongo.ts";
const app = express();
const server = createServer(app);
app.use(morgan("dev"));
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.set("views", "./views");
// app.use((req, res, next) => {
// const range = req.headers.range;
// if (range) {
// console.log("range ", range);
// next();
// } else {
// next();
// }
// });
app.use((req, res, next) => {
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, private");
next();
})
app.get("/home-page", async (req, res) => {
console.log(req.baseUrl);
if (!process.env.MONGO_URI) {
return res.status(500).send("URI not found");
}
const bucket = await getBucket(process.env.MONGO_URI, "videos");
const data = await bucket.find({}).toArray();
return res.render("homepage", {
videos: data,
});
});
app.use("/upload", uploadRoutes);
app.use("/video", videoRoutes);
app.use("/stream", streamRoutes);
app.use("*", (req, res) => {
res.status(404).send("Page not found");
});
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).send("Something went wrong");
});
server.listen(process.env.PORT ?? 3000, () => {
console.log("Server started on port " + (process.env.PORT ?? 3000));
});