-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
35 lines (28 loc) · 854 Bytes
/
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
'use strict'
const express = require('express')
const errors = require('./libs/responseErrors')
const app = express()
//Get item with id = 1
app.get('/items/1', (req, res, next) => {
res.json({name: 'Foo', description: 'Bar'})
})
//Create new item
app.post('/items', (req, res, next) => {
//Failed token validation
next(new errors.InvalidTokenError())
})
//Handle all other requests as Forbidden
app.use((req, res, next) => {
next(new errors.ForbiddenError())
})
//Error handler
app.use((err, req, res, next) => {
const statusCode = err.status || 500
const type = err.type || 'UnknownError'
const message = err.message || 'Something went wrong.'
res.status(statusCode).json({type, message})
})
//Start server
const server = app.listen(3000, () => {
console.log('Listening on port %s', server.address().port)
})