-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
45 lines (35 loc) · 1.02 KB
/
index.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
require('dotenv').config();
const assert = require('assert');
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const winston = require('winston');
assert(process.env.PORT, 'process.env.PORT is missing.');
winston.add(new winston.transports.Console());
const app = express();
app.set('port', process.env.PORT);
app.use(
cors({
origin: (origin, callback) => {
if (
!origin ||
(process.env.CORS_ALLOWLIST || '').split(',').includes(origin)
) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS.'));
}
},
})
);
app.use(morgan('common'));
app.get('/', (req, res) => {
res.json({ message: `The timestamp is ${new Date().toISOString()}.` });
});
// eslint-disable-next-line no-unused-vars
app.use((error, req, res, next) => {
res.status(500).json({ message: error.message });
});
const server = app.listen(app.get('port'), () => {
winston.info(`Listening on port ${server.address().port}...`);
});