-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (35 loc) · 1.3 KB
/
index.js
File metadata and controls
41 lines (35 loc) · 1.3 KB
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
const express = require('express')
const dotenv = require('dotenv')
const mongoose = require('mongoose')
const morgan = require("morgan")
const helmet = require("helmet")
const bodyparser = require('body-parser')
//import routes
const {authRoute,userRoute,postRoute,feedbackRoute} = require('./src/routes')
dotenv.config()
const app = express()
//setting up the port
const PORT = process.env.PORT || 5000
//allows express to read the body and then parse that into a Json object
app.use(bodyparser.json())
app.use((req,res,next)=>{
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,DELETE,PUT');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Origin, Accept');
next()
})
//route middleware
app.use('/api/auth',authRoute)
app.use('/api/users',userRoute)
app.use('/api/posts',postRoute)
app.use('/api/feedback',feedbackRoute)
app.use(morgan('common'))
app.use(helmet())
app.use('/',async(req,res)=>{
res.status(200).send('server is connected. this is / endpoint')
})
//connnect to MongoDB database with mongoose library
//app listen to the port for localhost server
mongoose.connect(process.env.MONGO).then(()=>{
app.listen(PORT,()=>console.log(`server and database has been connected to port:${PORT}`))
}).catch(e=>console.log(e))