-
Notifications
You must be signed in to change notification settings - Fork 56
/
server.js
59 lines (48 loc) · 1.41 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
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const MongoDBURI = process.env.MONGO_URI || 'mongodb://localhost/ManualAuth';
mongoose.connect(MongoDBURI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
});
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/views'));
const index = require('./routes/index');
app.use('/', index);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send(err.message);
});
// listen on port 3000
app.listen(process.env.PORT || 3000, () => {
console.log('Express app listening on port 3000');
});