generated from BuildforSDG-Cohort1-Assessment/covid-19-estimator-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (39 loc) · 1.43 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
46
47
48
49
// eslint-disable-next-line import/no-extraneous-dependencies
// import 'source-map-support/register';
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
// const fs = require('fs');
// Express Controllers
const estimator = require('./src/app/estimator');
const estimatorXml = require('./src/app/estimatorXml');
const logsEndpoint = require('./src/app/logs');
// Express Middlewares
const responseTime = require('./src/app/middlewares/responseTime');
// Initiating Main Express app
const app = express();
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Log File
const logFile = path.join(__dirname, 'logs.txt');
// Delete log File at startup
// const logFileExist = fs.existsSync(logFile);
// if (logFileExist) {
// fs.unlinkSync(logFile);
// }
// Middlewares
app.use(responseTime(logFile));
// The API endpoints
app.post('/api/v1/on-covid-19', estimator);
app.post('/api/v1/on-covid-19/json', estimator);
app.post('/api/v1/on-covid-19/xml', estimatorXml);
app.get('/api/v1/on-covid-19/logs', logsEndpoint(logFile));
// 404 endpoint
app.use((req, res) => {
res.status(404).send('Page not found');
});
const port = process.env.PORT || 80;
app.listen(port, () => {
console.log('Node Server Started', `Listening at Port: ${port}`);
});