-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
165 lines (138 loc) · 5.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Environment variable config see README.env for example
require('dotenv').config();
// server setup
const cluster = require('cluster');
const debug = require('debug')('quiddity:server');
const http = require('http');
const numCPUs = require('os').cpus().length;
const port = process.env.PORT || '3000';
// express/next setup
const express = require('express');
const { join } = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev });
const handle = app.getRequestHandler();
// server routes
const api = require('./server/routes/api');
app.prepare().then(() => {
const server = express();
// pages
const index = require('./server/routes/index');
const about = require('./server/routes/about');
const articles = require('./server/routes/articles');
// security - like to not display the backend is built on express ;)
server.disable('x-powered-by');
// server auth - if you want to password protect a staging site here's a nice easy way to do it
if (process.env.BASIC_AUTH_ENABLED === 'true') {
const auth = require('basic-auth');
let admins = {};
admins[process.env.BASIC_AUTH_USERNAME] = {password: process.env.BASIC_AUTH_PASSWORD};
const authenticate = (req, res, next) => {
const user = auth(req);
if (!user || !admins[user.name] || admins[user.name].password !== user.pass) {
res.set('WWW-Authenticate', 'Basic realm="Access to application"');
return res.status(401).send();
}
req.user = user;
return next();
};
server.use(authenticate);
}
// logs
server.use(logger('dev'));
// configs
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
// setup server routes
server.use('/api', api);
// pages
server.get('/', index.index(app));
server.get('/about', about.index(app));
server.get('/articles', articles.index(app));
server.get('/articles/:id', articles.get(app));
// next/js routes that don't require backend routes
server.get('*', (req, res) => {
// setup static files from root like sitemap.xml || robots.txt || favicon.ico
const parsedUrl = parse(req.url, true);
const rootStaticFiles = [
'/robots.txt',
'/sitemap.xml',
'/favicon.ico'
];
if (rootStaticFiles.indexOf(parsedUrl.pathname) > -1) {
const path = join(__dirname, 'static', parsedUrl.pathname);
return app.serveStatic(req, res, path);
}
// else serve page if not required by server
return handle(req, res);
});
if (dev) {
// if in development don't use cluster api since this causes webpack hot reload to behave erratically
server.listen(port, (err) => {
if (err) throw err
console.log('> Development server ready on http://localhost:3000')
});
}
else {
// cluster api for production only
server.set('port', port);
// setup workers for concurrency
if (cluster.isMaster) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// If a worker dies, log it to the console and start another worker.
cluster.on('exit', (worker, code, signal) => {
console.log('Worker ' + worker.process.pid + ' died.');
cluster.fork();
});
// Log when a worker starts listening
cluster.on('listening', (worker, address) => {
console.log('Worker started with PID ' + worker.process.pid + '.');
});
}
else {
//Create HTTP server.
let ns = http.createServer(server);
// Listen on provided port, on all network interfaces.
ns.listen(port);
ns.on('error', (error) => {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
});
ns.on('listening', () => {
const addr = ns.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
});
}
}
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});