forked from holidayextras/jsonapi-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (56 loc) · 1.72 KB
/
server.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const server = module.exports = { }
const jsonApi = require('../.')
const fs = require('fs')
const path = require('path')
const debug = require('debug')
jsonApi.setConfig({
graphiql: true,
swagger: {
title: 'Example JSON:API Server',
version: '0.1.1',
description: 'This is the API description block that shows up in the swagger.json',
contact: {
name: 'API Contact',
email: '[email protected]',
url: 'docs.hapi.holidayextras.com'
},
license: {
name: 'MIT',
url: 'http://opensource.org/licenses/MIT'
}
},
protocol: 'http',
hostname: 'localhost',
port: 16006,
base: 'rest',
meta: {
description: 'This block shows up in the root node of every payload'
}
})
jsonApi.authenticate((request, callback) => {
// If a "blockMe" header is provided, block access.
if (request.headers.blockme) return callback('Fail')
// If a "blockMe" cookie is provided, block access.
if (request.cookies.blockMe) return callback('Fail')
return callback()
})
fs.readdirSync(path.join(__dirname, '/resources')).filter(filename => /^[a-z].*\.js$/.test(filename)).map(filename => path.join(__dirname, '/resources/', filename)).forEach(require)
jsonApi.onUncaughtException((request, error) => {
const errorDetails = error.stack.split('\n')
console.error(JSON.stringify({
request,
error: errorDetails.shift(),
stack: errorDetails
}))
})
jsonApi.metrics.on('data', data => {
debug('metrics')(data)
})
// If we're using the example server for the test suite,
// wait for the tests to call .start();
if (typeof describe === 'undefined') {
jsonApi.start()
}
server.start = jsonApi.start
server.close = jsonApi.close
server.getExpressServer = jsonApi.getExpressServer