-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (28 loc) · 946 Bytes
/
Copy pathserver.js
File metadata and controls
37 lines (28 loc) · 946 Bytes
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
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//Connecting Database
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Database Connected Successfully"))
.catch(error => console.log("ERROR : Database Connection Failed", error));
//Routing
const indexRouter = require('./Routes/index');
app.use('/', indexRouter);
//Error Handling
app.use((req, res, next) => {
const error = new Error("Not Found");
error.http_code = 404;
next(error);
})
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: error.message,
status: error.status
})
})
const PORT = process.env.PORT || 5000;
app.listen(PORT,console.log(`Listening at Port : ${PORT}`));