-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
56 lines (45 loc) · 1.44 KB
/
server.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
// Todo: add compression: https://www.npmjs.com/package/compression
import express, { Express, Request, Response } from "express";
import helmet from "helmet";
import morgan from "morgan";
import http from "http";
import path from "path";
// Import routes
import indexRouter from "./indexRouter";
// The server
const app: Express = express();
// Middleware
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginResourcePolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false,
})
);
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan("combined"));
// View engine
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
//* Favicon
app.get("/favicon.ico", (_req: Request, res: Response) => {
res.sendFile(path.join(__dirname + "/public/favicon.png"));
});
// // Test Route
// app.get("/test", (req: Request, res: Response) => {
// console.log("req.ip:", req.ip);
// res.send("<h1 style='color:blue;text-align:center'>Server is running</h1>");
// });
//Route middleware
app.use("/", indexRouter);
// Port
const port = (process.env.PORT || 5000) as number;
const server = http.createServer(app);
server.listen({ port: port }, () => {
console.log(`Server is listening at http://localhost:${port}`);
// For testing only
console.log("Current Time:", new Date().toLocaleTimeString());
});