-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
56 lines (42 loc) · 1.27 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express')
const app =express()
const mongoose = require('mongoose')
const PORT = 5000
const {MONGOURI} = require('./Keys')
mongoose.connect(MONGOURI,{
useNewUrlParser: true,
useUnifiedTopology:true,
useFindAndModify:false
})
mongoose.connection.on('connected',()=> {
console.log("Connected to MongoDB")
})
mongoose.connection.on('error',(err)=> {
console.log("Error Connecting", err)
})
// Registering Models
require('./models/user')
require('./models/post')
//Express server never automatically parses json requests, so for that we need to do the following.
app.use(express.json())
// Registering Routes
app.use(require('./routes/auth'))
app.use(require('./routes/post'))
//Custom Middleware: This will be called first before any of the call routes is being called.
// const CustomMiddleware = (req,res,next)=>{
// console.log("Middleware Executed")
// next()
// }
// app.use(CustomMiddleware)
// app.get('/',(req,res)=>{
// console.log("Home")
// res.send("hello world")
// })
//If you want to add a middleware to a specific route
// app.get('/about',CustomMiddleware,(req,res)=>{
// console.log("About")
// res.send("About Page")
// })
app.listen(PORT,()=>{
console.log("Server is running on:", PORT)
})