-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (30 loc) · 947 Bytes
/
app.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 bodyParser = require('body-parser');
const Cors = require('cors');
const path = require('path');
var app = express();
const port = 3000;
const route = require('../contactList/routes/route');
//bodyparser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
//static files
app.use(express.static(path.join(__dirname,'public')));
//cors
app.use(Cors());
//connecting with mongodb
mongoose.connect('mongodb://localhost:27017/admin',{useUnifiedTopology: true, useNewUrlParser: true});
//on erro
mongoose.connection.on('error',()=>{
console.log("error while connecting to the database");
});
//on connection
mongoose.connection.on('connected',()=>{
console.log("connected to the database");
});
//forwarding all routes if /api/----
app.use('/api',route);
app.listen(3000,()=>{
console.log("Server started at port :"+port);
});