-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
grab-ngrok.js
38 lines (33 loc) · 836 Bytes
/
grab-ngrok.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
// @ts-check
const fs = require('fs');
const http = require('http');
const ngrokErr = new Error('Could not parse Ngrok response. Is Ngrok running? Is port 4040 being blocked?');
// Query ngrok local API
const req = http.get('http://127.0.0.1:4040/api/tunnels', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// Extract public URL
try {
const ngrokTunnelInfo = JSON.parse(data);
// Assume first tunnel
const publicUrl = ngrokTunnelInfo.tunnels[0].public_url;
// Update config.js file
const configText = `
module.exports = {
destination: '${publicUrl}'
}
`;
fs.writeFileSync('./config.js', configText);
console.log('config updated!');
return publicUrl;
} catch (e) {
throw ngrokErr;
}
});
});
req.on('error', (err) => {
throw ngrokErr;
})