-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·69 lines (56 loc) · 1.79 KB
/
Copy pathserver.js
File metadata and controls
executable file
·69 lines (56 loc) · 1.79 KB
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
import helmet from 'helmet';
import express from 'express';
import winston from 'winston';
import { MongoClient } from 'mongodb';
import expressWinston from 'express-winston';
import routes from './routes';
const host = `${process.env.MONGODB_REPLICA_NODE_1},${process.env.MONGODB_REPLICA_NODE_2},${process.env.MONGODB_REPLICA_NODE_3}`;
const replicaSet = process.env.MONGODB_REPLICA_SET;
const useSsl = process.env.MONGODB_SSL;
const db = process.env.MONGODB_DATABASE;
const user = process.env.MONGODB_USERNAME;
const pass = process.env.MONGODB_PASSWORD;
const mongoDbUrl = `mongodb://${user}:${pass}@${host}/${db}?ssl=${useSsl}&replicaSet=${replicaSet}&authSource=admin`;
const app = express();
MongoClient.connect(mongoDbUrl, (error, mongoPool) => {
if (error) {
console.error(error);
return;
}
app.mongoPool = mongoPool;
// Helmet helps securing Express.js apps by setting various HTTP headers
// https://github.com/helmetjs/helmet
app.use(helmet());
// Request logger
// https://github.com/bithavoc/express-winston
app.use(expressWinston.logger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
})
],
meta: true,
msg: "HTTP {{req.method}} {{req.url}}",
expressFormat: true,
colorize: true,
ignoreRoute: function (req, res) { return false; }
}));
// Mount public routes
app.use('/', express.static(`${__dirname}/public`));
const apiPrefix = '/api/';
app.use(apiPrefix, routes);
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
})
]
}));
const nodePort = 3333;
app.listen(nodePort, () => {
console.info(`Restful API server is listening on port ${nodePort}`);
});
});
export default app;