-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (42 loc) · 1.52 KB
/
index.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
// modules/config/index.js
"use strict";
const express = require('express');
const bodyParser = require('body-parser');
module.exports = async (app) => {
// create an express app for the REST server
const restServer = express();
// configure restServer to use bodyParser()
// this will let us get the data from a POST
restServer.use(bodyParser.urlencoded({ extended: false }));
restServer.use(bodyParser.json());
// get an instance of the express Router
const router = express.Router();
// setup the (by now) only get route on /
router.get('/', (req, res) => app.gateway ?
res.json({ attributes: app.attributes, values: app.gateway }) :
res.json({ attributes: app.attributes, values: app.device.values })
);
router.post('/', (req, res) => {
const { body } = req;
if (app.gateway) {
Object.keys(app.attributes.gateway).forEach(attr => {
if (body.hasOwnProperty(attr)) {
app.attributes.gateway[attr] = body[attr];
}
});
} else {
Object.keys(app.attributes).forEach(attr => {
if (body.hasOwnProperty(attr)) {
app.attributes[attr] = body[attr];
}
});
}
res.end();
});
// all of our routes will be prefixed with /
restServer.use('/', router);
// Listen for requests
restServer.listen(4001, () => {
app.logger.info(`REST api started on port ${4001}`);
});
};