-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (35 loc) · 1.17 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
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
const apiRouter = require('./routes/api');
const cors = require('cors');
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development';
const { db_url } =
process.env.NODE_ENV === 'production'
? process.env
: require('./config')[process.env.NODE_ENV];
mongoose.connect(db_url).then(() => console.log(`connected to ${db_url}`));
app.use(bodyParser.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', apiRouter);
app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, './public/html/index.html'));
});
app.get('/*', (req, res, next) => {
next({ status: 404 });
});
app.use((err, req, res, next) => {
if (err.status === 404) {
res.status(404).send({ message: 'Page not found' });
} else if (err.status === 400) {
res.status(400).send({ message: 'Bad Request' });
} else next(err);
});
app.use((err, req, res, next) => {
console.log(err);
res.status(500).send({ message: 'Internal Server Error' });
});
module.exports = app;