-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (34 loc) · 950 Bytes
/
server.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
41
42
43
44
45
46
47
48
49
50
var express = require('express');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var cors = require('cors');
var path = require('path');
var app = express();
const route = require('./routes/routes')
// connect to mongodb
mongoose.connect('mongodb://localhost:27017/contactlist',{useNewUrlParser: true});
// on connection
mongoose.connection.on('connected', ()=>{
console.log('connected to database mongodb @27017')
})
mongoose.connection.on('error', (err)=>{
if(err){
console.log('Error in Database connection',err);
}
});
// port no
const port = 3000;
// adding middleware - cors
app.use(cors());
// body parser
app.use(bodyparser.json());
app.use('/api', route);
// static files
app.use(express.static(path.join(__dirname, 'public')));
// testing server
app.get('/', (req, res)=>{
res.send('foobar');
})
app.listen(port,()=>{
console.log('server started at port: '+port);
});