-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (55 loc) · 1.54 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const methodOverride = require('method-override');
const cors = require('cors');
require('./api/middleware/db');
require('./api/middleware/passport');
const api = require('./api/routes');
const app = express();
const isProduction = process.env.NODE_ENV === 'production';
const isTest = process.env.NODE_ENV === 'test';
app.use(cors());
app.use(helmet());
if (!isTest) app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res, next) => {
res.status(200).json({
message: 'You have reached the library api',
});
});
app.use('/api', api);
app.use((_req, _res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
if (!isProduction) {
app.use((err, _req, res, _next) => {
res.status(err.status || 500).json({
error: {
message: err.message,
error: err,
},
});
});
}
app.use((err, _req, res, _next) => {
res.status(err.status || 500).json({
error: {
message: err.message,
},
});
});
app.set('port', process.env.PORT || 3000);
const server = app.listen(app.get('port'), () => {
console.log(`Server listening on port: ${server.address().port}`);
});
module.exports = app;