-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (29 loc) · 1.11 KB
/
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
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const database = require('./database')
const eventModel = require('./event/model')
const eventRouter = require('./event/router')
// Creata an express API server app
const app = express()
// API server port
const port = 4000
// Enable Cross-Origin-Resource-Sharing
// cors() returns a middleware
// cors() needs to add the header 'Access-Control-Allow-Origin' first
const corsMiddleware = cors()
// The order matters: make sure you register cors() before all other middlewares and routes
app.use(corsMiddleware) // Add header field 'Access-Control-Allow-Origin: *'
const parserMiddleware = bodyParser.json()
// The request body must be parsed before the request is handled.
// Otherwise, the JSON body will be unreadable.
app.use(parserMiddleware) // Parse request json body
app.use(eventRouter)
// GET request test
app.get('/test', (req, res) => {
res.send("Backend server is working now!")
})
// To start API server
app.listen(port, () => {
console.log(`RESTful API server is listening on port ${port}`)
})