-
Notifications
You must be signed in to change notification settings - Fork 0
/
myserv.js
44 lines (40 loc) · 1.26 KB
/
myserv.js
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
const express = require("express");
const app = express();
const logEvents = require("./middleware/logger");
const path = require("path");
const PORT = process.env.PORT || 3000;
//
app.use((req, res, next) => {
logEvents(`${req.method}\t${req.headers.origin}\t${req.url} `, "thelog.txt");
console.log(`${req.method} ${req.path} `);
next();
});
//built-in middlewares in express js
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(path.join(__dirname, "/public")));
// Configuring successfull routing mechanism including regex patterns
app.get("^/$|/index(.html)?", (req, res) => {
res.sendFile(path.join(__dirname, "views", "index.html"));
});
app.get("/new-page.html", (req, res) => {
res.sendFile(path.join(__dirname, "views", "new-page.html"));
});
const one = (req, res, next) => {
console.log("one");
next();
};
const two = (req, res, next) => {
console.log("two");
next();
};
const three = (req, res, next) => {
console.log("three");
res.send("Finished");
};
app.get("/chain(.html)?", [one, two, three]);
app.get("/*", (req, res) => {
console.log(req.url);
res.status(404).sendFile(path.join(__dirname, "views", "404.html"));
});
app.listen(PORT, () => console.log("Server is running on port " + PORT));