-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (51 loc) · 1.5 KB
/
server.js
File metadata and controls
65 lines (51 loc) · 1.5 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require('express');
const hbs = require('hbs');
const port = process.env.PORT || 3000;
var app = express();
// set partial path
hbs.registerPartials(__dirname + '/views/partials');
// Registering helper , see call of helper in footer.hbs
hbs.registerHelper('getCurrentYear',()=>{
return new Date().getFullYear();
});
// set view engine handlebars
app.set('view engine','hbs');
// set defaulut template/html directory using middleware
app.use(express.static(__dirname + '/public'));
// middleware, executed before any request served.
app.use((request,response,next)=>{
console.log('Time:' + new Date().toString() +', Request Method: ' + request.method );
next();
});
app.get('/',(request,response) => {
// sending HTML or plain text
// response.send('<h1>Hello Express</h1>');
// sending JSON response
// response.send({
// name:"Bhavin",
// hobbies: ["Programming","Eating"]
// });
// rendering template
response.render('home.hbs',{
pageTitle:'Home Page',
welcomeMessage: "Welcome to my website."
});
});
app.get('/about',(request,response)=>{
response.render('about.hbs',{
pageTitle:'About Page'
});
});
app.get('/projects',(request,response)=>{
response.render('projects.hbs',{
pageTitle:'Projects Page'
});
});
app.get('/bad',(request,response)=>{
response.send({
error:'Unable to find the request'
});
});
app.listen(port, ()=> {
console.log("server is running");
});