Server
​
1
require('dotenv').config();
2
const express = require('express');
3
const axios = require('axios');
4
const bodyParser = require('body-parser');
5
const pino = require('express-pino-logger')();
6
​
7
const app = express();
8
app.use(bodyParser.urlencoded({ extended: false }));
9
app.use(pino);
10
​
11
app.get('/api/get-speech-token', async (req, res, next) => {
12
res.setHeader('Content-Type', 'application/json');
13
const speechKey = process.env.SPEECH_KEY;
14
const speechRegion = process.env.SPEECH_REGION;
15
​
16
if (speechKey === 'paste-your-speech-key-here' || speechRegion === 'paste-your-speech-region-here') {
17
res.status(400).send('You forgot to add your speech key or region to the .env file.');
18
} else {
19
const headers = {
20
headers: {
21
'Ocp-Apim-Subscription-Key': speechKey,
22
'Content-Type': 'application/x-www-form-urlencoded'
23
}
24
};
25
​
26
try {
27
const url =
28
"https://speech-westus-speaker.cognitiveservices.azure.com/sts/v1.0/issuetoken"; //
29
const tokenResponse = await axios.post(
30
`https://${speechRegion}.api.cognitive.microsoft.com/sts/v1.0/issueToken`,
31
null,
32
headers
33
);
34
res.send({ token: tokenResponse.data, region: speechRegion });
35
} catch (err) {
36
res.status(401).send('There was an error authorizing your speech key.');
37
}
38
}
39
});
40
​
41
app.listen(3001, () =>
42
console.log('Express server is running on localhost:3001')
43
);
Copied!
​
Export as PDF
Copy link