-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (30 loc) · 1016 Bytes
/
server.js
File metadata and controls
36 lines (30 loc) · 1016 Bytes
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
const express = require('express');
const request = require('request');
const app = express();
const port = 80;
app.get('/synthesize', (req, res) => {
const apiKey = 'YANDEX_CLOUD_SECRET_KEY_API';
const text = req.query.text || 'Unfortunately there is no text';
const lang = 'ru-RU';
const url = `https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize?text=${encodeURIComponent(text)}&lang=${lang}&format=oggopus&sampleRateHertz=48000&speed=1.0`;
const requestOptions = {
uri: url,
method: 'GET',
headers: {
'Authorization': `Api-Key ${apiKey}`,
},
encoding: null
};
request(requestOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
res.setHeader('Content-Type', 'audio/ogg');
res.send(body);
} else {
console.error('Error:', error || body);
res.status(500).send('There was an error in speech synthesis');
}
});
});
app.listen(port, () => {
console.log(`Server listening on port: ${port}`);
});