-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (30 loc) · 910 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
const express = require("express");
const app = express();
const morgan = require("morgan");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
dotenv.config();
// Databases
mongoose
.connect(process.env.MONGO_URI, { useNewUrlParser: true })
.then(() => console.log("DB Connected"));
mongoose.connection.on("error", (err) => {
console.log(`DB connection error: ${err.message}`);
});
// Routes
const postRoutes = require("./routes/post");
// Middleware
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.get("/", function (req, res) {
res.send('BACKEND CARI TIM NIH BOS')
});
app.use("/posts", postRoutes);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`A node.js API is listening to port: ${port}`);
});