forked from AndreasAakesson/lb_elle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers.js
28 lines (25 loc) · 938 Bytes
/
servers.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
var http = require('http');
var stringToColour = function(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
//We need a function which handles requests and send response
function handleRequest(request, response){
response.setTimeout(5000);
var addr = request.connection.localPort;
var bgcolor = stringToColour(addr + "42");
var body = '<h1>'+ addr +'</h1><br/>' + 'Link established with server <strong>' + addr + '</strong>';
var page = "<html><head></head><body bgcolor=\""+ bgcolor +"\">" + body + "</body></html>";
response.end(page);
}
http.createServer(handleRequest).listen(8090);
http.createServer(handleRequest).listen(8091);
http.createServer(handleRequest).listen(8092);