-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (54 loc) · 1.9 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
60
61
62
63
64
65
66
67
const express = require('express');
const logger = require('morgan');
const news = require('./routes/news');
const users = require('./routes/users');
const errorHandler = require('./errorHandler');
const bodyParser = require('body-parser');
const mongoose = require('./database');
const fs = require('fs');
const session = require('express-session')
const app = express();
const passport = require('./passportHelper').initPassport();
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
});
app.use(logger('common', { stream: fs.createWriteStream('./access.log', { flags: 'a' }) }))
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(session({ secret: 'secret', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
app.use('/news', news);
app.use('/users', users);
app.post('/login',
passport.authenticate('local'),
function (req, res) {
res.status(200).json();
});
app.get('/login/google',
passport.authenticate('google', {
scope: [,
'openid']
}));
app.get('/login/google/callback',
passport.authenticate('google'),
function (req, res) {
res.render('hello.jade', { title: 'Hello', name: req.user.name, authData: req.user });
});
app.use(errorHandler.internalError);
app.use(errorHandler.notFound);
if (process.env.NODE_ENV !== 'test') {
app.listen(3000, function () {
console.log('Server on port 3000 started');
});
}
module.exports = app;