-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (33 loc) · 977 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
const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const app = express();
// Initialize env variables
require("dotenv").config({ path: "process.env" });
// Connect to database
mongoose.connect(process.env.MONGO_DB, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on(
"error",
console.error.bind(console, "Connection error:")
);
mongoose.connection.once("open", () => console.log("Connected to database"));
// Initialize middleware
app.use(morgan("dev"));
app.use(express.json());
// Routes
app.use("/api/books", require("./routes/index"));
// Handle 404 errors
app.use((req, res, next) => {
res.status(404).json({ msg: "Not found!" });
});
// Handle all errors
app.use((err, req, res) => {
res.status(err.status || 500).json({ error: err.message });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}...`);
});