-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (44 loc) · 1.51 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
require('dotenv').config();
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const mime = require('mime');
const path = require('path');
const cookieParser = require('cookie-parser');
const methodOverride = require('method-override');
const connectDB = require('./config/dbConn');
const authRouter = require('./routes/auth');
const mainRouter = require('./routes/main');
const unAuthRouter = require('./routes/unauthorized');
PORT = process.env.PORT || 3000;
connectDB();
// set the views engine to ejs
app.set('view engine', 'ejs');
// use cookieParser to parse cookie to the client request object
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
// enable use of static files in express (i.e. scripts,styles)
app.use(express.static(path.join(__dirname, 'public')));
// use method override to send PUT and DELETE request
app.use(methodOverride('_method'));
// use a middleware that checks for the extention and sets a proper content-type in the header
app.use((req, res, next) => {
if (req.url.endsWith('.js')) {
res.setHeader('Content-Type', mime.getType(req.url));
}
next();
});
app.get('/', (req, res) => {
const option = {
sign: 'sign-up',
log: 'log-in',
};
res.render('index', { option: option });
});
app.use('/auth', authRouter);
app.use('/main', mainRouter);
app.use('/unauthorized', unAuthRouter);
mongoose.connection.once('open', () => {
console.log('Connected to DB');
app.listen(PORT, console.log(`Server running on port ${PORT}`));
});