-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
58 lines (51 loc) · 1.39 KB
/
app.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
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
const path = require('path');
const express = require('express');
const app = express();
require('./config/express')(app);
// Create Text to Speech client.
let client;
try {
client = new TextToSpeechV1({});
} catch (err) {
console.error('Error creating service client: ', err);
}
app.get('/', (_, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/health', (_, res) => {
res.json({ status: 'UP' });
});
app.get('/api/voices', async (_, res, next) => {
try {
const { result } = await client.listVoices();
return res.json(result);
} catch (err) {
console.error(err);
if (!client) {
err.statusCode = 401;
err.description =
'Could not find valid credentials for the Text to Speech service.';
err.title = 'Invalid credentials';
}
next(err);
}
});
app.get('/api/synthesize', async (req, res, next) => {
try {
const { result } = await client.synthesize(req.query);
result.pipe(res);
} catch (err) {
console.error(err);
if (!client) {
err.statusCode = 401;
err.description =
'Could not find valid credentials for the Text to Speech service.';
err.title = 'Invalid credentials';
}
next(err);
}
});
// error-handler settings for all other routes
require('./config/error-handler')(app);
module.exports = app;