-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
73 lines (60 loc) · 2.63 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
68
69
70
71
72
73
'use strict';
var express = require('express');
var morgan = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var busboy = require('connect-busboy');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var dotenv = require('dotenv');
var server = express();
/* ====================================================== */
// Load environment variables from .env file
dotenv.load();
/* ====================================================== */
server.use(morgan('dev')); // Logs all requests to the console
server.use(compression()); // Compresses response data with gzip/deflate
server.use(methodOverride()); // Simulates DELETE and PUT
server.use(bodyParser.json()); // Parses req.body json from html POST
server.use(bodyParser.urlencoded({ extended: true })); // Parses urlencoded req.body, including extended syntax
server.use(busboy()); // Parse multipart/form-data
server.use(cookieParser());
server.set('json spaces', 0); // Remove superfluous spaces from JSON responses
server.use(session({
secret: process.env.SECRET,
cookie: {
maxAge: 1000*60*30 // only 30 minutes until user logs in
},
resave: false,
saveUninitialized: false
}));
server.use(passport.initialize());
server.use(passport.session());
/* ====================================================== */
// Add headers
server.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
/* ====================================================== */
// Mount the API
server.use('/api/v1', require('./api'));
/* ====================================================== */
// Respond with 404 to any routes not matching API endpoints
server.all('/*', function(req, res) {
res.json({ status: 404, message: 'No endpoint exists at ' + req.originalUrl });
});
/* ====================================================== */
// start the server
server.listen(process.env.PORT || 3000);