-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (28 loc) · 1.03 KB
/
server.js
File metadata and controls
39 lines (28 loc) · 1.03 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
const express = require('express')
const bodyParser = require('body-parser')
const api = require('./api')
const auth = require('./auth')
const middleware = require('./middleware')
const port = process.env.PORT || 1337
const app = express()
app.disable('x-powered-by')
app.use(middleware.cors)
app.use(bodyParser.json())
app.get('/health', api.checkHealth)
if (process.env.NODE_ENV !== 'test') app.use(middleware.logger)
app.get('/products', api.listProducts)
app.get('/products/:id', api.getProduct)
app.post('/products', auth.ensureUser, api.createProduct)
app.put('/products/:id', auth.ensureUser, api.editProduct)
app.delete('/products/:id', auth.ensureUser, api.deleteProduct)
app.get('/orders', auth.ensureUser, api.listOrders)
app.post('/orders', auth.ensureUser, api.createOrder)
app.use(middleware.handleValidationError)
app.use(middleware.handleError)
app.use(middleware.notFound)
const server = app.listen(port, () =>
console.log(`Server listening on port ${port}`)
)
if (require.main !== module) {
module.exports = server
}