-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (32 loc) · 1.08 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
if (!process.env.NODE_ENV) process.env.NODE_ENV = "dev";
const CORS = require("cors");
const express = require("express");
const app = express();
const apiRouter = require("./routes/index");
const mongoose = require("mongoose");
const config = require("./config");
const bodyParser = require("body-parser");
let db = process.env.DB || config.DB[process.env.NODE_ENV];
mongoose.Promise = Promise;
mongoose
.connect(db)
.then(() => console.log(`successfully connected to...${db}`))
.catch(err => console.log("connection failed", err));
app.use(CORS());
app.use(bodyParser.json());
app.use("/api", apiRouter);
app.use("/*", (req, res, next) =>
res.status(404).send({ status: 404, msg: "Page not found" })
);
app.use((err, req, res, next) => {
if (err.status === 404) {
res.status(404).send({ status: 404, msg: "Page not found" });
} else next(err);
});
app.use((err, req, res, next) => {
if (err.status === 400)
res.status(400).send({ status: 400, msg: "Bad request" });
else next(err);
});
app.use((err, req, res, next) => res.status(500).send(err));
module.exports = app;