-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (37 loc) · 1.1 KB
/
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
// Importing Express Framework
var express = require("express");
var bodyParser = require('body-parser');
// Import Mongoose
let mongoose = require('mongoose');
// Intializing the app
var app = express();
// Import Route Module
var apiRoutes = require('./api-routes');
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Setup server port
var port = process.env.PORT || 3000;
app.use('/api', apiRoutes);
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true});
var db = mongoose.connection;
// Added check for DB connection
if(!db)
console.log("Error connecting db")
else
console.log("Db connected successfully")
// Sending message to the default Route
app.get('/', function (req, res) {
res.json({
status: 'Main page',
message: 'Welcome to the world of RESTAPI',
description: 'Goto /api to explore API'
});
});
// Launch app to listen to the specified PORT
app.listen(3000, () => {
console.log(`Server running on port ${port}`);
});