-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 921 Bytes
/
index.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
require("dotenv").config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const mongoose = require ("mongoose");
const cors = require('cors');
const goalRouter = require("./routes/goalRouter");
//middlewares
app.use(express.json());
app.use(cors());
//routes
app.get("/", (req, res) => {
res.status(200).json({message: "Welcome to Goals API"});
});
// /api/goals
app.use ("/api/goals", goalRouter);
//error route
app.use((req, res) => {
res.status(404).json({message:'Resource Not Found'});
});
//data base connection and server listening mongoose
const startServer = async () => {
try {
await mongoose.connect(process.env.MONGO_URL, {dbName: 'goalServer'})
app.listen(PORT, ()=> {
console.log(`Server running on port: ${PORT}...`);
});
} catch (error) {
console.log(error);
}
};
startServer();