-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
31 lines (26 loc) · 851 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
31
require("dotenv").config();
const express = require("express");
const Ably = require("ably");
const app = express();
const ably = new Ably.Rest({ key: process.env.ABLY_API_KEY });
app.use(express.static("public"));
app.get("/", (request, response) => {
response.sendFile(__dirname + "/views/index.html");
});
app.get("/auth", (req, res) => {
let tokenParams = {
ttl: 30000,
clientId: req.query.id,
};
ably.auth.createTokenRequest(tokenParams, (err, tokenRequest) => {
if (err) {
res.status(500).send("Authentication error: " + JSON.stringify(err));
} else {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(tokenRequest));
}
});
});
const listener = app.listen(process.env.PORT, function () {
console.log("Your app is listening on port " + listener.address().port);
});