-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (35 loc) · 1.05 KB
/
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
const express = require('express')
const app= express()
const mongoose= require('mongoose')
//CREATING SERVER
const port= process.env.Port || 3000;
//when hosted then random port is assigned
// so to check if 3000 is available we use
//process.env.Port
//DATABASE CONNECTION
const url='mongodb://localhost/node-api'
mongoose.connect(url,{
useNewUrlParser:true,
useCreateIndex:true,
useUnifiedTopology:true,
useFindAndModify:true
})
//above connecton is formed b/w compass and file
const connecton=mongoose.connection
//to check if database is connected we can use the event
connecton.once('open', ()=>{
console.log('database connected')
}).catch((err)=>{
console.log('databse error');
})
app.use(express.json());
//fetch routes file
const articlesRoutes= require('./routes/articles')
//articlesRoutes contains all the routes
app.use('/api/articles', articlesRoutes)
//now articlesRoutes containes only that routes whose
//prefix is /api/articles
//second parameter for port is function
app.listen(port, ()=> {
console.log(`listening on port ${port}`)
})