-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·128 lines (106 loc) · 5.1 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'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');
// express-handlebars is a templating library
// https://www.npmjs.com/package/express-handlebars
// - look inside the views folder for the templates
// data is inserted into a template inside {{ }}
const hbs = require('express-handlebars');
// request is used to make REST calls to the backend microservice
// details here: https://www.npmjs.com/package/request
var request = require('request');
// create the server
const app = express();
// set up handlbars as the templating engine
app.set('view engine', 'hbs');
app.engine('hbs', hbs({
extname: 'hbs',
defaultView: 'default'
}));
// set up the parser to get the contents of data from html forms
// this would be used in a POST to the server as follows:
// app.post('/route', urlencodedParser, (req, res) => {}
const urlencodedParser = bodyParser.urlencoded({ extended: false });
// defines a route that receives the request to /
app.get('/', (req, res) => {
// make a request to the backend microservice using the request package
// the URL for the backend service should be set in configuration
// using an environment variable. Here, the variable is passed
// to npm start inside package.json:
// "start": "SERVER=http://localhost:8082 node server.js",
request.get( // first argument: url + return format
{
url: process.env.SERVER + '/events', // the microservice end point for events
json: true // response from server will be json format
}, // second argument: function with three args,
// runs when server response received
// body hold the return from the server
(error, response, body) => {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log(body); // print the return from the server microservice
res.render('home',
{
layout: 'default', //the outer html page
template: 'index-template', // the partial view inserted into
// {{body}} in the layout - the code
// in here inserts values from the JSON
// received from the server
events: body.events
}); // pass the data from the server to the template
});
});
// defines a route that receives the post request to /event
app.post('/event',
urlencodedParser, // second argument - how to parse the uploaded content
// into req.body
(req, res) => {
// make a request to the backend microservice using the request package
// the URL for the backend service should be set in configuration
// using an environment variable. Here, the variable is passed
// to npm start inside package.json:
// "start": "SERVER=http://localhost:8082 node server.js",
request.post( // first argument: url + data + formats
{
url: process.env.SERVER + '/event', // the microservice end point for adding an event
body: req.body, // content of the form
headers: { // uploading json
"Content-Type": "application/json"
},
json: true // response from server will be json format
},
(error, response, body) => { // third argument: function with three args,
// runs when server response received
// body hold the return from the server
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log(body); // print the return from the server microservice
res.render('home',
{
layout: 'default', //the outer html page
template: 'index-template', // the partial view inserted into
// {{body}} in the layout - the code
// in here inserts values from the JSON
// received from the server
events: body.events
}); // pass the data from the server to the template
});
});
// create other get and post methods here - version, login, etc
// generic error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: err.message });
});
// specify the port and start listening
const PORT = 8080;
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;