-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (25 loc) · 851 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
const express = require('express');
const routes = require('./routes/api');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
//setting up the app
const app = express();
var admin='<Username?>:<PassWord?>';
//connecting to mongoDB
mongoose.connect(`mongodb+srv://${admin}@cluster-kyo.vqnss.mongodb.net/REST_API?retryWrites=true&w=majority`)
mongoose.Promise = global.Promise;
//front-end static requests
app.use(express.static('public'));
//using body-parser as a middleware
app.use(bodyParser.json());
//intializing routes
app.use('/api',routes);
//Error handling middleware
app.use(function(err,req,res,next){
//console.log(err);
res.status(422).send({error: err._message});
})
//listening to a port
app.listen(process.env.PORT || 4000, function(){
console.log('Express has setup successfully!');
})