-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (39 loc) · 1.12 KB
/
app.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
45
46
47
const express = require("express");
const logger = require("morgan");
const methodOverride = require("method-override");
const cookieParser = require ("cookie-parser");
const path = require("path");
// REQUIRE ROUTE FILES
const welcomeRouter = require("./routes/welcome");
const clucksRouter = require("./routes/clucks");
// CREATE APP
const app = express();
// CHOOSE TEMPLATING ENGINE
app.set("view engine", "ejs");
// SETUP MIDDLEWARE
app.use(logger("dev"));
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(cookieParser())
// Method Override
app.use(
methodOverride((req, res) => {
if (req.body && req.body._method) {
const method = req.body._method;
return method;
}
})
);
app.use((req,res,next)=>{
res.locals.username= req.cookies.username || ""
next()
})
// USE ROUTERS FROM ROUTE FILES
app.use("/", welcomeRouter);
app.use("/clucks", clucksRouter);
// START SERVER
const PORT = 4000;
const DOMAIN = "localhost";
app.listen(PORT, DOMAIN, () => {
console.log(`💁 Server is listening at http://${DOMAIN}:${PORT}`);
});