@@ -26,24 +26,25 @@ app.use(express.static(__dirname + '/public'));
2626app . use ( express . static ( __dirname + '/../dist' ) ) ; // normally these files would also go into public/ but this way the example always has the latest code
2727
2828
29+ // set up an endpoint to serve speech-to-text auth tokens
30+
2931// For local development, replace username and password
30- var config = extend ( {
32+ var sttConfig = extend ( {
3133 version : 'v1' ,
3234 url : 'https://stream.watsonplatform.net/speech-to-text/api' ,
3335 username : '<username>' ,
3436 password : '<password>'
3537} , vcapServices . getCredentials ( 'speech_to_text' ) ) ;
3638
3739// quick hack to make development easier
38- try { extend ( config , require ( '../test/resources/stt-auth.json' ) ) } catch ( ex ) { }
39-
40- var authService = watson . authorization ( config ) ;
40+ try { extend ( sttConfig , require ( '../test/resources/stt-auth.json' ) ) } catch ( ex ) { }
4141
42+ var sttAuthService = watson . authorization ( sttConfig ) ;
4243
4344// Get token using your credentials
44- // **Warning**: this endpoint should be guarded with additional authentication & authorization for production use
45- app . get ( '/token' , function ( req , res , next ) {
46- authService . getToken ( { url : config . url } , function ( err , token ) {
45+ // **Warning**: these endpoints should be guarded with additional authentication & authorization for production use
46+ app . get ( '/api/speech-to-text/ token' , function ( req , res ) {
47+ sttAuthService . getToken ( { url : sttConfig . url } , function ( err , token ) {
4748 if ( err ) {
4849 console . log ( 'Error retrieving token: ' , err ) ;
4950 return res . status ( 500 ) . send ( 'Error retrieving token' )
@@ -52,12 +53,35 @@ app.get('/token', function(req, res, next) {
5253 } ) ;
5354} ) ;
5455
56+ // and, do it all again for the text to speech service
57+ var ttsConfig = extend ( {
58+ version : 'v1' ,
59+ url : 'https://stream.watsonplatform.net/text-to-speech/api' ,
60+ username : '<username>' ,
61+ password : '<password>'
62+ } , vcapServices . getCredentials ( 'text_to_speech' ) ) ;
63+
64+ // quick hack to make development easier
65+ try { extend ( ttsConfig , require ( '../test/resources/tts-auth.json' ) ) } catch ( ex ) { }
66+
67+ var ttsAuthService = watson . authorization ( ttsConfig ) ;
68+
69+ app . get ( '/api/text-to-speech/token' , function ( req , res ) {
70+ ttsAuthService . getToken ( { url : ttsConfig . url } , function ( err , token ) {
71+ if ( err ) {
72+ console . log ( 'Error retrieving token: ' , err ) ;
73+ return res . status ( 500 ) . send ( 'Error retrieving token' )
74+ }
75+ res . send ( token ) ;
76+ } ) ;
77+ } ) ;
78+
5579var port = process . env . VCAP_APP_PORT || 3000 ;
5680app . listen ( port , function ( ) {
57- console . log ( 'Example IBM Watson STT client app & server live at http://localhost:%s/' , port ) ;
81+ console . log ( 'Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/' , port ) ;
5882} ) ;
5983
60- // chrome requires https to access the user's mic
84+ // chrome requires https to access the user's mic unless it's a localhost url
6185if ( ! process . env . VCAP_APP_PORT ) {
6286 var fs = require ( "fs" ) ,
6387 https = require ( "https" ) ,
@@ -68,6 +92,6 @@ if (!process.env.VCAP_APP_PORT) {
6892 cert : fs . readFileSync ( __dirname + '/keys/localhost.cert' )
6993 } ;
7094 https . createServer ( options , app ) . listen ( HTTPS_PORT , function ( ) {
71- console . log ( 'Secure example IBM Watson STT client app & server live at https://localhost:%s/' , port )
95+ console . log ( 'Secure server live at https://localhost:%s/' , port )
7296 } ) ;
7397}
0 commit comments