-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·68 lines (56 loc) · 1.89 KB
/
app.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
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const passport = require('passport');
const flash = require('express-flash');
const User = require('./models/user');
const cors = require('cors');
const morgan = require("morgan");
// load .env
require("dotenv").config();
// connect mongoDB
mongoose.connect(
'mongodb://localhost:27017',
{ useNewUrlParser: true, useUnifiedTopology: true }
);
// Passport Authentication
const initializePassport = require('./passport-config');
initializePassport(
passport,
async email => {return User.findOne({email: email})},
async id => {return User.findById(id)});
/**
* middleware
* note: middleware is running in sequence, from top to bottom
*/
app.use(bodyParser.json()); // parse client request data to json format
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(flash());
app.use(passport.initialize());
app.use(cors());
app.use(morgan('dev'));
// import routers
const auth = require('./routes/auth');
const car = require('./routes/car');
const parking = require('./routes/parking');
const payment = require('./routes/payment');
const session = require('./routes/session');
// apply router middleware
app.use('/auth', auth);
app.use('/car', passport.authenticate('jwt', { session: false }), car);
app.use('/parking', passport.authenticate('jwt', { session: false }), parking);
app.use('/payment', passport.authenticate('jwt', { session: false }), payment);
app.use('/session', passport.authenticate('jwt', { session: false }), session);
/**
* Get port from environment and store in Express.
*/
const PORT = process.env.PORT || 5000;
/**
* Listen on provided PORT, on all network interfaces.
*/
app.listen(PORT, ()=>{
console.log(`Server running on http://localhost:${PORT}`)
});