-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (33 loc) · 1.63 KB
/
server.js
File metadata and controls
42 lines (33 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const Ably = require('ably');
const Express = require('express');
const ServerPort = process.env.PORT || 3000;
const worker = require('./worker');
const ApiKey = process.env.ABLY_API_KEY || 'INSERT-YOUR-API-KEY-HERE'; /* Add your API key here */
if (ApiKey.indexOf('INSERT') === 0) { throw('Cannot run without an Ably API key. Add your key to server.js'); }
const WolframAppId = process.env.WOLFRAM_APP_ID || 'INSERT-YOUR-WOLFRAM-APP-ID-HERE'; /* Add your Wolfram AppID here */
if (WolframAppId.indexOf('INSERT') === 0) { console.warn('Cannot run without a Wolfram AppID. Add your AppID to server.js'); }
/* Instance the Ably library */
const rest = new Ably.Rest({ key: ApiKey });
/* Start a web server */
const app = Express();
/* Issue token requests to browser clients sending a request to the /auth endpoint */
app.get('/auth', function (req, res) {
rest.auth.createTokenRequest(function(err, tokenRequest) {
if (err) {
res.status(500).send('Error requesting token: ' + JSON.stringify(err));
} else {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(tokenRequest));
}
});
});
if (WolframAppId.indexOf('INSERT') === 0) {
app.get('/', function (req, res) {
res.status(500).send('WolframAppId is not set. You need to configure an environment variable WOLFRAM_APP_ID');
});
}
/* Server static HTML files from /public folder */
app.use(Express.static('public'));
app.listen(ServerPort);
worker.start(ApiKey, WolframAppId, 'wolfram:answers', 'wolfram', 'us-east-1-a-queue.ably.io:5671/shared');
console.log('Open the Wolfram demo in your browser: https://localhost:' + ServerPort + '/');