forked from KoleMyers/apple-musickit-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (32 loc) · 1.24 KB
/
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
32
33
34
35
36
37
38
#!/usr/bin/env node
const express = require('express');
const app = express();
const hostname = process.env.HOSTNAME || 'localhost';
const port = parseInt(process.env.PORT, 10) || 8080;
const publicDir = process.argv[2] || __dirname + '/public';
const path = require('path');
// library for signing tokens
const jwt = require('jsonwebtoken');
const fs = require('fs');
app.get('/', function (req, res) {
res.sendFile(path.join(publicDir, '/index.html'));
});
const private_key = fs.readFileSync('apple_private_key.p8').toString(); // read your private key from your file system
const team_id = 'ABCDEFGHIJ'; // your 10 character apple team id, found in https://developer.apple.com/account/#/membership/
const key_id = 'KLMNOPQRST'; // your 10 character generated music key id. more info https://help.apple.com/developer-account/#/dev646934554
const token = jwt.sign({}, private_key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: team_id,
header: {
alg: 'ES256',
kid: key_id
}
});
app.get('/token', function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({token: token}));
});
app.use(express.static(publicDir));
console.log('Listening at', publicDir, hostname, port);
app.listen(port, hostname);