-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 1006 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
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const todoHandler = require('./routeHandler/todoHandler');
// express app initialization
const app = express();
dotenv.config();
app.use(express.json())
const dbUrl = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@cluster0.0idnwn5.mongodb.net/${process.env.DB_DATABASE_NAME}?retryWrites=true&w=majority`;
// database connect with mongoose
mongoose.connect(dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {console.log("Connection successful.")})
.catch((err) => {console.log(err)})
app.use('/todo', todoHandler);
app.get('/', (req, res) => {
res.send("hello world.");
})
// default error handler
const errorHandler = (err, req, res, next) => {
if(res.headersSent){
next(err);
} else {
res.status(500).json({error: err});
}
}
app.listen(3000, () => {
console.log("Listening to port 3000");
})