/
server.js
30 lines (24 loc) · 970 Bytes
/
server.js
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
const Ably = require('ably');
const Express = require('express');
const ServerPort = 3000;
const ApiKey = 'INSERT-YOUR-API-KEY-HERE'; /* Add your API key here */
if (ApiKey.indexOf('INSERT') === 0) { throw('Cannot run without an API key. Add your key to server.js'); }
/* Instance the Ably library */
const rest = new Ably.Rest({ key: ApiKey });
/* Start a web server */
var 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));
}
});
});
/* Server static HTML files from /public folder */
app.use(Express.static('public'));
app.listen(3000);
console.log('Web server listening on port', ServerPort);