-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (33 loc) · 910 Bytes
/
index.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
var express=require("express")
var app=express()
var bodyParser=require("body-parser")
var authRoutes=require("./routes/auth")
var eventRoutes=require("./routes/events")
var errorHandler=require("./handlers/error")
var db=require("./models")
const port=process.env.PORT || 8080;
app.use('/',express.static('public'))
app.use(bodyParser.json())
app.get("/api/events",async function(req,res,next){
try{
let events=await db.Event.find()
.sort({createdAt:"desc"})
.populate("user",{
username:true,
})
return res.status(200).json(events)
}catch(e){
return next(e)
}
})
app.use("/api/auth",authRoutes)
app.use("/api/users/:id/events",eventRoutes)
app.use((req,res,next)=>{
let err=new Error("NOT FOUND")
err.status=404
next(err)
})
app.use(errorHandler)
app.listen(port,()=>{
console.log(`SERVER STARTED ON ${port}`)
})