-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-server.1.txt
85 lines (67 loc) · 1.89 KB
/
node-server.1.txt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const express = require('express');
const winston = require('winston');
const helmet = require('helmet');
const nodeProxy = require('./node-proxy');
const nodeAppServer = require('./node-app-server');
const authPassport = require('./auth-passport');
const bodyParser = require('body-parser');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
let users;
/**
* Heroku-friendly production http server.
*
* Serves your app and allows you to proxy APIs if needed.
*/
const app = express();
const PORT = process.env.PORT || 8080;
authPassport.readUsers()
.then( (_users) => {
users = _users;
})
.catch( (err) => {
throw err;
});
// Enable various security helpers.
app.use(helmet());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(
(username, password, done) => {
authPassport.authenticateUser(username, password, users)
.then( (authResult) => {
return done(null, authResult);
})
.then(null, (message) => {
return done(null, false, message);
});
}
));
passport.serializeUser( (user, done) => {
done(null, user.meta.id);
});
passport.deserializeUser( (id, done) => {
done(null, authPassport.getUserById(id, users));
});
app.post('/api/auth/login',
passport.authenticate('local'),
(req, res) => {
res.status(200).send(JSON.stringify(req.user));
}
);
// API proxy logic: if you need to talk to a remote server from your client-side
// app you can proxy it though here by editing ./proxy-config.js
nodeProxy(app);
// Serve the distributed assets and allow HTML5 mode routing. NB: must be last.
nodeAppServer(app);
// Start up the server.
app.listen(PORT, (err) => {
if (err) {
winston.error(err);
return;
}
winston.info(`Listening on port ${PORT}`);
});