-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
97 lines (86 loc) · 2.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
* Copyright (c) 2016 Nicola Del Gobbo
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the license at http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
* IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
* MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing
* permissions and limitations under the License.
*
* Contributors - initial API implementation:
* Nicola Del Gobbo <[email protected]>
******************************************************************************/
'use strict'
/*!
* Module dependencies
*/
const express = require('express')
const responseTime = require('response-time')
const responsePoweredBy = require('response-powered-by')
const http = require('http')
const bodyParser = require('body-parser')
const errorhandler = require('errorhandler')
const morgan = require('morgan')
const cors = require('cors')
module.exports = function createServer() {
const app = express()
// Set express server port
app.set('port', process.env.PORT || 5000)
app.use(morgan('dev'))
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.use(responsePoweredBy("@NickNaso"))
app.use(responseTime())
// https://www.npmjs.com/package/cors#enabling-cors-pre-flight
app.options('*', cors())
/**
* Routes for the application
*/
app
.get('/resources', (req, res) => {
res.status(200).json([
{
id: 1,
first_name: "Nicola",
last_name: "Del Gobbo"
},
{
id: 2,
first_name: "Dennis",
last_name: "Ritchie"
}
])
})
.post('/resources', (req, res) => {
res.status(201).json({
id: 4,
first_name: "Ryan",
last_name: "Dahl"
})
})
.put('/resources/:id', (req, res) => {
res.status(200).json({
id: 1,
first_name: "Nick",
last_name: "Naso"
})
})
.delete('/resources/:id', (req, res) => {
res.status(200).json({
id: 1,
first_name: "Nick",
last_name: "Naso"
})
})
app.use(errorhandler())
// Create http server and attach express app on it
return http.createServer(app).listen(app.get('port'), '0.0.0.0', () => {
console.log("Server started at http://localhost:" + app.get('port') + "/")
})
}