20
20
21
21
const express = require ( 'express' ) ;
22
22
const app = express ( ) ;
23
+ const watson = require ( 'watson-developer-cloud' ) ;
24
+ const vcapServices = require ( 'vcap_services' ) ;
23
25
const expressBrowserify = require ( 'express-browserify' ) ;
26
+ const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
27
+ const webpack = require ( 'webpack' ) ;
28
+ const webpackConfig = require ( './webpack.config' ) ;
24
29
25
30
// allows environment properties to be set in a file named .env
26
31
require ( 'dotenv' ) . load ( { silent : true } ) ;
@@ -66,12 +71,7 @@ app.get(
66
71
) ;
67
72
68
73
// 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
-
73
74
const compiler = webpack ( webpackConfig ) ;
74
-
75
75
app . use (
76
76
webpackDevMiddleware ( compiler , {
77
77
publicPath : '/' // Same as `output.publicPath` in most cases.
@@ -80,18 +80,60 @@ app.use(
80
80
81
81
// token endpoints
82
82
// **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
+ } ) ;
85
127
86
128
const port = process . env . PORT || process . env . VCAP_APP_PORT || 3000 ;
87
129
app . listen ( port , function ( ) {
88
130
console . log ( 'Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/' , port ) ;
89
131
} ) ;
90
132
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
93
135
// 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
95
137
if ( ! process . env . VCAP_SERVICES ) {
96
138
const fs = require ( 'fs' ) ;
97
139
const https = require ( 'https' ) ;
0 commit comments