-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·78 lines (65 loc) · 2.42 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
'use strict';
// express is a nodejs web server
// https://www.npmjs.com/package/express
const express = require('express');
// converts content in the request into parameter req.body
// https://www.npmjs.com/package/body-parser
const bodyParser = require('body-parser');
// create the server
const app = express();
// the backend server will parse json, not a form request
app.use(bodyParser.json());
// allow AJAX calls from 3rd party domains
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, MERGE, GET, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
// mock events data - for a real solution this data should be coming
// from a cloud data store
const mockEvents = {
events: [
{ title: 'an event', id: 1, description: 'something really cool' },
{ title: 'another event', id: 2, description: 'something even cooler' }
]
};
// health endpoint - returns an empty array
app.get('/', (req, res) => {
res.json([]);
});
// version endpoint to provide easy convient method to demonstrating tests pass/fail
app.get('/version', (req, res) => {
res.json({ version: '1.0.0' });
});
// mock events endpoint. this would be replaced by a call to a datastore
// if you went on to develop this as a real application.
app.get('/events', (req, res) => {
res.json(mockEvents);
});
// Adds an event - in a real solution, this would insert into a cloud datastore.
// Currently this simply adds an event to the mock array in memory
// this will produce unexpected behavior in a stateless kubernetes cluster.
app.post('/event', (req, res) => {
// create a new object from the json data and add an id
const ev = {
title: req.body.title,
description: req.body.description,
id : mockEvents.events.length + 1
}
// add to the mock array
mockEvents.events.push(ev);
// return the complete array
res.json(mockEvents);
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: err.message });
});
const PORT = process.env.PORT ? process.env.PORT : 8082;
const server = app.listen(PORT, () => {
const host = server.address().address;
const port = server.address().port;
console.log(`Events app listening at http://${host}:${port}`);
});
module.exports = app;