-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (54 loc) · 1.76 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
- Defining path (routs) of different pages and linking them
- Getting data from form
- dynamic genrating html content
-dynamic routes
*/
const path = require("path");
// path package for absolute path
const express = require("express");
// express
const defaultRoutes = require("./routes/default");
// importing default routes from 'default'
const restaurantsRoutes = require("./routes/restaurants");
// importing default routes from 'default'
let port = 3000;
if (process.env.PORT) {
port = process.env.PORT;
}
const app = express();
// calling express as it a function
app.set("views", path.join(__dirname, "views"));
// setting views setting i.e where to find tamplate files
app.set("view engine", "ejs");
// setting a tamplating engine 'ejs'
// that helps us genrate dynamic html
// CHANGE HTML FILE FORMAT TO 'ejs'
// i.e index.ejs
app.use(express.static("public"));
// if user tryes to access page through diff paths
// it sends file if that path file is present in public folder
// need to change in html path --> /route what you have defined
app.use(express.urlencoded({ extended: false }));
// checking data from page
app.use("/", defaultRoutes);
// will look for the routes requested starting with'/'
//in the defaultRoutes that we impotred if not found
// will look in app.js down
app.use("/", restaurantsRoutes);
/*
sending html file as response
app.get("/index", function (req, res) {
const htmlFilePath = path.join(__dirname, "views", "index.html");
res.sendFile(htmlFilePath);
// sending html files as erlier we were typing html code as string
});
*/
app.use(function (req, res) {
res.status(404).render("404");
});
// backend error handling
app.use(function (error, req, res, next) {
res.status(500).render("500");
});
app.listen(port);