Skip to content

Commit dab6e2d

Browse files
committed
simplify node.js server example
1 parent 0fb68c1 commit dab6e2d

File tree

3 files changed

+52
-80
lines changed

3 files changed

+52
-80
lines changed

examples/server.js

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
const express = require('express');
2222
const app = express();
23+
const watson = require('watson-developer-cloud');
24+
const vcapServices = require('vcap_services');
2325
const expressBrowserify = require('express-browserify');
26+
const webpackDevMiddleware = require('webpack-dev-middleware');
27+
const webpack = require('webpack');
28+
const webpackConfig = require('./webpack.config');
2429

2530
// allows environment properties to be set in a file named .env
2631
require('dotenv').load({ silent: true });
@@ -66,12 +71,7 @@ app.get(
6671
);
6772

6873
// set up webpack-dev-middleware to serve Webpack bundles for examples
69-
const webpackDevMiddleware = require('webpack-dev-middleware');
70-
const webpack = require('webpack');
71-
const webpackConfig = require('./webpack.config');
72-
7374
const compiler = webpack(webpackConfig);
74-
7575
app.use(
7676
webpackDevMiddleware(compiler, {
7777
publicPath: '/' // Same as `output.publicPath` in most cases.
@@ -80,18 +80,60 @@ app.use(
8080

8181
// token endpoints
8282
// **Warning**: these endpoints should probably be guarded with additional authentication & authorization for production use
83-
app.use('/api/speech-to-text/', require('./stt-token.js'));
84-
app.use('/api/text-to-speech/', require('./tts-token.js'));
83+
84+
// speech to text token endpoint
85+
var sttAuthService = new watson.AuthorizationV1(
86+
Object.assign(
87+
{
88+
url: watson.SpeechToTextV1.URL,
89+
username: process.env.SPEECH_TO_TEXT_USERNAME, // or hard-code credentials here
90+
password: process.env.SPEECH_TO_TEXT_PASSWORD
91+
},
92+
vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
93+
)
94+
);
95+
app.use('/api/speech-to-text/token', function(req, res) {
96+
sttAuthService.getToken({}, function(err, token) {
97+
if (err) {
98+
console.log('Error retrieving token: ', err);
99+
res.status(500).send('Error retrieving token');
100+
return;
101+
}
102+
res.send(token);
103+
});
104+
});
105+
106+
// text to speech token endpoint
107+
var ttsAuthService = new watson.AuthorizationV1(
108+
Object.assign(
109+
{
110+
url: watson.TextToSpeechV1.URL,
111+
username: process.env.TEXT_TO_SPEECH_USERNAME, // or hard-code credentials here
112+
password: process.env.TEXT_TO_SPEECH_PASSWORD
113+
},
114+
vcapServices.getCredentials('text_to_speech') // pulls credentials from environment in bluemix, otherwise returns {}
115+
)
116+
);
117+
app.use('/api/text-to-speech/token', function(req, res) {
118+
ttsAuthService.getToken({}, function(err, token) {
119+
if (err) {
120+
console.log('Error retrieving token: ', err);
121+
res.status(500).send('Error retrieving token');
122+
return;
123+
}
124+
res.send(token);
125+
});
126+
});
85127

86128
const port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
87129
app.listen(port, function() {
88130
console.log('Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/', port);
89131
});
90132

91-
// chrome requires https to access the user's microphone unless it's a localhost url so
92-
// this sets up a basic server at https://localhost3001/ using an included self-signed certificate
133+
// Chrome requires https to access the user's microphone unless it's a localhost url so
134+
// this sets up a basic server on port 3001 using an included self-signed certificate
93135
// note: this is not suitable for production use
94-
// however bluemix automatically adds https support at http://<myapp>.mybluemix.net
136+
// however bluemix automatically adds https support at https://<myapp>.mybluemix.net
95137
if (!process.env.VCAP_SERVICES) {
96138
const fs = require('fs');
97139
const https = require('https');

examples/stt-token.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/tts-token.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)