-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
44 lines (36 loc) · 989 Bytes
/
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
const path = require('path');
const express = require('express');
const app = express();
const morgan = require('morgan');
const cors = require('cors');
const favicon = require('serve-favicon');
//body-parser
app.use(express.urlencoded({extended: false}));
app.use(express.json());
//route files
const translateRoutes = require('./routes/translate');
//dev logging middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//enable CORS
app.use(cors());
//set static folder
app.use(express.static(path.join(__dirname, '_public')));
app.use(favicon(path.join(__dirname, '_public', 'favicon.ico')));
//mount routers
app.use('/', translateRoutes);
//error handler
app.use((req,res,next) => {
const error = new Error('Not Found!');
error.status = 400;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).json({
error: {
message: error.message,
}
})
});
module.exports = app;