This repository was archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (34 loc) · 1.4 KB
/
server.js
File metadata and controls
44 lines (34 loc) · 1.4 KB
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 hbs = require("hbs");
var app = express(); //initializing express
hbs.registerPartials(__dirname + "/views/partials");
hbs.registerHelper("getCurrentYear", () => new Date().getFullYear());
app.set("view engine", "hbs");
app.use(express.static(__dirname + "/public"));// we serve the ENTIRE public as static content.
/* MIDDLEWARE */
app.use((request, response, next) => {
//Every request passes through this in the order it is done;
let now = new Date().toString();
console.log(`${now}: ${request.method} ${request.url}`)
next(); // Only passes through next middlewere once this method is called successfully or the response is sent!
});
//Setting http handlers
app.get("/", (request, response) => {
let params = {
welcomeMessage: "Hello this is the homepage",
pageTitle: "Home Page",
};
response.render("home.hbs", params);
});
app.get("/about", (request, response) => {
response.render("about.hbs", {
pageTitle: "About Page",
}) // rendering the template and passing values to the template
});
app.get("/bad", (request, response) => {
response.send({ errorMessage: "Page not found"}); //Express automatically adjust the content type and serve it
});
let port = process.env.PORT || 3000;
app.listen(process.env.PORT, () => {
console.log("Server is up at port 3000");
}); // bind the app to a port and start to listen