-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
69 lines (58 loc) · 1.96 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
'use strict'
var express = require('express')
var app = express()
app.set("port", process.env.PORT || 4000)
app.get('/', function (req, res) {
console.log('GET request received')
res.writeHead(200, {'Content-Type': 'application/json'})
var response = { "response" : "This is GET method." }
console.log(response)
res.end(JSON.stringify(response))
})
app.get('/:id', function (req, res) {
console.log('GET /:id request received')
res.writeHead(200, {'Content-Type': 'application/json'})
var response = { "response" : "This is GET method with id=" + req.params.id + "." }
console.log(response)
res.end(JSON.stringify(response))
})
app.post('/', function (req, res) {
console.log('POST request received')
res.writeHead(200, {'Content-Type': 'application/json'})
var response = { "response" : "This is POST method." }
console.log(response)
res.end(JSON.stringify(response))
})
app.put('/', function (req, res) {
console.log('PUT request received')
res.writeHead(200, {'Content-Type': 'application/json'})
var response = { "response" : "This is PUT method." }
console.log(response)
res.end(JSON.stringify(response))
})
app.delete('/', function (req, res) {
console.log('DELETE request received')
res.writeHead(200, {'Content-Type': 'application/json'})
var response = { "response" : "This is DELETE method." }
console.log(response)
res.end(JSON.stringify(response))
})
const server = app.listen(app.get("port"), function () {
const host = server.address().address
const port = server.address().port
console.log("Node.js API app running at http://%s:%s", host, port)
})
process.on('SIGINT', () => {
console.log('Received SIGINT. Closing server...')
server.close(() => {
console.log('Server closed. Exiting process...')
process.exit(0)
})
})
process.on('SIGTERM', () => {
console.log('Received SIGTERM. Closing server...')
server.close(() => {
console.log('Server closed. Exiting process...')
process.exit(0)
})
})