forked from IBM/nodejs-express-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·48 lines (37 loc) · 1.31 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
37
38
39
40
41
42
43
44
45
46
47
48
// import dependencies and initialize express
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import helmet from 'helmet';
import { fileURLToPath } from 'url';
import healthRoutes from './routes/health-route.js';
import swaggerRoutes from './routes/swagger-route.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// if production, enable helmet
/* c8 ignore next 3 */
if (process.env.VCAP_APPLICATION) {
app.use(helmet());
}
// 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'));
});
export default app;