@@ -25,6 +25,29 @@ const expressBrowserify = require('express-browserify');
25
25
// allows environment properties to be set in a file named .env
26
26
require ( 'dotenv' ) . load ( { silent : true } ) ;
27
27
28
+ // on bluemix, enable rate-limiting and force https
29
+ if ( process . env . VCAP_SERVICES ) {
30
+ // enable rate-limiting
31
+ const RateLimit = require ( 'express-rate-limit' ) ;
32
+ app . enable ( 'trust proxy' ) ; // required to work properly behind Bluemix's reverse proxy
33
+
34
+ const limiter = new RateLimit ( {
35
+ windowMs : (
36
+ 15 * 60 * 1000
37
+ ) , // 15 minutes
38
+ max : 100 , // limit each IP to 100 requests per windowMs
39
+ delayMs : 0 // disable delaying - full speed until the max limit is reached
40
+ } ) ;
41
+
42
+ // apply to /api/*
43
+ app . use ( '/api/' , limiter ) ;
44
+
45
+ // force https - microphone access requires https in Chrome and possibly other browsers
46
+ // (*.mybluemix.net domains all have built-in https support)
47
+ const secure = require ( 'express-secure-only' ) ;
48
+ app . use ( secure ( ) ) ;
49
+ }
50
+
28
51
app . use ( express . static ( __dirname + '/static' ) ) ;
29
52
30
53
// set up express-browserify to serve browserify bundles for examples
@@ -57,35 +80,12 @@ app.use(
57
80
} )
58
81
) ;
59
82
60
- // on bluemix, enable rate-limiting and force https
61
- if ( process . env . VCAP_SERVICES ) {
62
- // enable rate-limiting
63
- const RateLimit = require ( 'express-rate-limit' ) ;
64
- app . enable ( 'trust proxy' ) ; // required to work properly behind Bluemix's reverse proxy
65
-
66
- const limiter = new RateLimit ( {
67
- windowMs : (
68
- 15 * 60 * 1000
69
- ) , // 15 minutes
70
- max : 100 , // limit each IP to 100 requests per windowMs
71
- delayMs : 0 // disable delaying - full speed until the max limit is reached
72
- } ) ;
73
-
74
- // apply to /api/*
75
- app . use ( '/api/' , limiter ) ;
76
-
77
- // force https - microphone access requires https in Chrome and possibly other browsers
78
- // (*.mybluemix.net domains all have built-in https support)
79
- const secure = require ( 'express-secure-only' ) ;
80
- app . use ( secure ( ) ) ;
81
- }
82
-
83
83
// token endpoints
84
84
// **Warning**: these endpoints should probably be guarded with additional authentication & authorization for production use
85
85
app . use ( '/api/speech-to-text/' , require ( './stt-token.js' ) ) ;
86
86
app . use ( '/api/text-to-speech/' , require ( './tts-token.js' ) ) ;
87
87
88
- const port = process . env . VCAP_APP_PORT || 3000 ;
88
+ const port = process . env . PORT || process . env . VCAP_APP_PORT || 3000 ;
89
89
app . listen ( port , function ( ) {
90
90
console . log ( 'Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/' , port ) ;
91
91
} ) ;
0 commit comments