-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (32 loc) · 1.04 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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const textToSpeech = require('./components/TextToSpeechCoordinator');
const pubSub = require('./components/PubSubCoordinator');
const app = express();
const PORT = process.env.PORT || 8080;
app.use(bodyParser.json());
const handleError = (error, res) => {
console.log(JSON.stringify(error));
res.status(500).send(error);
};
const handleSuccess = (res) => res.sendStatus(200);
app.post('/broadcast', (req, res) => {
var text = req.body.text;
var voice = req.body.voice;
textToSpeech
.generate(text, voice)
.then((payload) => {
pubSub
.broadcast('frontend', payload)
.then((pubResponse) => {
handleSuccess(res);
})
.catch((error) => handleError(error, res));
})
.catch((error) => handleError(error, res));
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}...`);
});
module.exports = app;