-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
43 lines (33 loc) · 1.31 KB
/
routes.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
const express = require('express')
, router = express.Router()
, needle = require('needle')
, util = require('util')
, fs = require('fs')
;
// Work with Promises all the time.
const appendFile = util.promisify(fs.appendFile);
router.get('/', (req, res) => {
res.render('home');
});
router.post('/intent', (req, res) => {
let userContent = req.body;
console.log('Intent request from editor:', JSON.stringify(userContent));
// TODO: Edit this URL accordingly.
let backendServer = 'http://localhost:5006/webhooks/myio/webhook';
return needle('post', backendServer, userContent, { json: true })
.then(response => res.json({ action: response.body }))
.catch(err => res.status(500).send(err));
});
router.post('/notify', (req, res) => {
// Don't log events if not running in production mode.
if (process.env.NODE_ENV != 'production') return res.sendStatus(418);
let userContent = req.body;
// Add timestamp, to ease back-tracing logs.
userContent.time = (new Date).getTime();
// We'll use NDJSON format to store the data, so add newline.
let line = JSON.stringify(userContent) + '\n';
return appendFile('events.ndjson', line)
.then(result => res.send(result))
.catch(err => res.status(500).send(err));
});
module.exports = router;