-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
58 lines (48 loc) · 1.19 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
"use strict";
require('dotenv').config()
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const models = require('./models');
/**
* Set static directory
*/
app.use(express.static('public'));
/**
* Parse JSON
*/
app.use(bodyParser.json());
/**
* Set up JWT middleware
*/
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
let opts = {
jwtFromRequest: ExtractJwt.fromBodyField('token'),
secretOrKey: process.env.SECRET_KEY
}
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
models.Agent.findOne({ id: jwt_payload.sub }, (err, agent) => {
if (err) {
return done(err, false);
}
if (agent) {
return done(null, agent);
}
else {
return done(null, false);
// or you could create a new account
}
});
}));
/**
* Routes
*/
const agent = require('./routes/agent');
app.use('/agent', agent);
let port = process.env.NODE_ENV === 'production' ? 3000 : 3001;
app.listen(port, '0.0.0.0', () => {
console.log('express-react-e2e-testing-demo listening on ' + port + '!');
});
module.exports = app;