-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (51 loc) · 1.56 KB
/
server.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
const developerNameDefault = "Michael B. Alao";
const developerCountryDefault = "Nigeria";
const time = new Date();
const express = require("express");
const bodyParser = require('body-parser');
const app = express();
// create handlebars with default layout
const handlebars = require('express-handlebars')
.create({ defaultLayout: 'main'});
app.engine('handlebars', handlebars.engine );
app.set('view engine', 'handlebars');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req,res,next){
res.render('home', {
layout: 'main',
developerName: developerNameDefault,
countryName: developerCountryDefault
});
});
app.get('/contact', function(req,res,next){
res.render('contact', {
layout: 'main',
developerName: developerNameDefault,
countryName: developerCountryDefault
});
});
// handling post from the form
app.post('/contact', function(req,res,next){
// Print all request to console
console.log(req.body);
res.render('thankyou', {
layout: 'main',
name: req.body.first_name,
time: time
} );
});
// Declaring handle for all error http here (after getting /post /put action ...etc)
// 404 not found
app.use(function(req,res) {
res.status(404);
res.render('404');
});
// 500 not found
app.use(function(err,req,res,next) {
console.log(err.stack);
res.status(500);
res.render('500');
});
const port = process.env.PORT || 3600;
app.listen(port, () => `Server running on port ${port} 🔥`);