-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (36 loc) · 1.18 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
'use strict';
const express = require('express');
const cookie = require('cookie-parser');
const mongoose = require('mongoose');
const helmet = require('helmet');
const morgan = require('morgan');
const app = express();
if (process.env.NODE_ENV === 'dev') {
require('dotenv').config();
}
const survey = require('./routes/survey');
const account = require('./routes/account');
const { ensureAuthenticated, notFound } = require('./middlewares');
app.use(helmet());
app.use(morgan('dev'));
app.use(express.json());
app.use(cookie(process.env.COOKIE_SECRET));
mongoose.connect(process.env.DATABASE, {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
}, error => { if (error) console.error(error); });
const db = mongoose.connection;
db.on('open', () => {
app.use('/account', account);
app.use('/survey', ensureAuthenticated, survey);
app.get('/', ensureAuthenticated, (req, res) => {
res.json({ message: "Hello from Sarvekshan.", id: res.locals.user._id });
});
app.use('/', notFound);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Sarvekshan is running on http://localhost:${PORT}`);
});