-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver3.js
63 lines (56 loc) · 1.93 KB
/
server3.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const http = require('http');
const https = require('https');
const fs = require('fs');
const querystring = require('querystring');
function processApiResponse(res, tableauServer, username, site) {
let data = {
ticket: '<ticket for \'' + username + '\'>'
};
let contents = JSON.stringify(data);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(contents + '\n');
console.log('200 POST /api - ' + contents);
}
function processRequest(req, callback) {
let chunks = [];
req.on('data', (chunk) => {
chunks.push(chunk);
}).on('end', () => {
let body = Buffer.concat(chunks).toString();
callback(querystring.parse(body));
});
}
http.createServer(function (req, res) {
if (req.method === 'GET') {
if (req.url === '/') {
let contents = fs.readFileSync('index.html', 'ascii');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(contents);
console.log('200 GET / - ' + contents.length);
} else {
res.writeHead(404);
res.end('Not Found');
console.log('404 GET ' + req.url);
}
} else if (req.method === 'POST') {
if (req.url === '/api') {
processRequest(req, function (data) {
let site = null;
console.log(' => ' + JSON.stringify(data));
let url = new URL(data['share_link']);
if (url.pathname.startsWith('/t/')) {
let tokens = url.pathname.split('/');
site = tokens[2];
}
processApiResponse(res, url.protocol + "//" + url.hostname, data['username'], site);
});
} else {
res.writeHead(404);
res.end('Not Found');
console.log('404 POST ' + req.url);
}
} else {
res.writeHead(405);
res.end('Invalid request method: ' + req.method);
}
}).listen(8080, '0.0.0.0');