-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (32 loc) · 1000 Bytes
/
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
"use strict";
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended : true }));
app.use(bodyParser.json());
app.get('/error', function(req, res) {
res.sendFile(path.join(__dirname, 'public', 'error.html'));
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'dist')));
app.use(function(err, req, res, next) {
console.error(err.message);
console.error(err.stack);
next(err);
});
app.use(function(err, req, res, next) {
if (req.accepts('html')) {
let message = encodeURIComponent(err.message);
res.redirect('/error?message=' + message);
} else {
res.status(500).send({error: err.message});
}
});
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const port = 2002;
app.listen(port, function () {
console.log(`listening on port ${port}`);
});