-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (62 loc) · 2.02 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import express from 'express';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
const port = 3000;
import { router as adminRouter } from './app/controllers/AdminController.js';
import { router as userRouter } from './app/controllers/UserController.js';
import { router as scRouter } from './app/controllers/SmartContractController.js';
import { router as spenderRouter } from './app/controllers/SpenderController.js';
import { router as swaggerRouter } from './app/configuration/swagger.js';
var app = express();
//import "dotenv/config.js";
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api/admin', adminRouter);
app.use('/api/user', userRouter);
app.use('/api/sc', scRouter);
app.use('/api/spender', spenderRouter);
app.use('/api/docs', swaggerRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
console.error(`Error! ${err}`);
const error = {
status: err.status || 500,
message: err.message,
};
res.status(error.status).send(error);
});
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
default:
throw error;
}
}
function onListening() {
const addr = app.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
console.log('\nListening on ' + bind);
}
app.listen(port);
app.on('error', onError);
app.on('listening', onListening);
console.log('Server started on port ' + port);