-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
76 lines (59 loc) · 1.77 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
65
66
67
68
69
70
71
72
73
74
75
76
//require('bootstrap');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const methodOverride = require('method-override');
const session = require('express-session');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 4666;
// SET view engine
app.set('view engine', 'ejs');
// Controllers
const ctrl = require('./controllers');
//-------- MIDDLEWARE ---------------//
//Serve Styling Files
app.use(express.static(`${__dirname}/public`));
// Express Session
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false, // Only save the session if a property changes
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 // expires in 24 hrs
}
}));
// Body Parser
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//Method Override
app.use(methodOverride('_method'));
// Logging middleware
app.use(morgan(':method : url'));
//Custom middleware
app.use((req, res, next) => {
const method = req.method;
const path = req.url;
const timestamp = new Date().toLocaleTimeString();
console.log(`${method} ${path} ${timestamp}`);
next(); // Allow the request to move on to the next middleware in the chain
});
app.get('/', (req, res) => {
res.render('home');
});
app.get('/aboutus', (req, res) => {
res.render('aboutus');
});
app.get('/contact', (req, res) => {
res.render('contact');
});
app.use('/businesses', ctrl.businesses);
app.use('/products', ctrl.products);
app.use('/search' , ctrl.search);
app.use('/auth',ctrl.login);
app.get('*', (req, res) => {
res.send('<h1> Requested page not found. </h1>');
});
app.listen(PORT, () => {
console.log(`Server is working on port: ${PORT}`);
});