forked from IBM/nodejs-express-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·36 lines (28 loc) · 1.06 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
// import dependencies and initialize express
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const healthRoutes = require('./routes/health-route');
const swaggerRoutes = require('./routes/swagger-route');
const app = express();
// enable parsing of http request body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// routes and api calls
app.use('/health', healthRoutes);
app.use('/swagger', swaggerRoutes);
// default path to serve up index.html (single page application)
app.all('', (req, res) => {
res.status(200).sendFile(path.join(__dirname, '../public', 'index.html'));
});
// start node server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App UI available http://localhost:${port}`);
console.log(`Swagger UI available http://localhost:${port}/swagger/api-docs`);
});
// error handler for unmatched routes or api calls
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, '../public', '404.html'));
});
module.exports = app;