Skip to content

Commit 460401c

Browse files
committed
example server tweaks
1 parent e980bec commit 460401c

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

examples/server.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ const expressBrowserify = require('express-browserify');
2525
// allows environment properties to be set in a file named .env
2626
require('dotenv').load({ silent: true });
2727

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+
2851
app.use(express.static(__dirname + '/static'));
2952

3053
// set up express-browserify to serve browserify bundles for examples
@@ -57,35 +80,12 @@ app.use(
5780
})
5881
);
5982

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-
8383
// token endpoints
8484
// **Warning**: these endpoints should probably be guarded with additional authentication & authorization for production use
8585
app.use('/api/speech-to-text/', require('./stt-token.js'));
8686
app.use('/api/text-to-speech/', require('./tts-token.js'));
8787

88-
const port = process.env.VCAP_APP_PORT || 3000;
88+
const port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
8989
app.listen(port, function() {
9090
console.log('Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/', port);
9191
});

0 commit comments

Comments
 (0)