forked from Labomc/xPlacesMobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileServer.js
56 lines (49 loc) · 1.38 KB
/
fileServer.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
/**
* Basic file server written in Node.
Displays requested pages and creates a xpMobileNode (thus a WebSocket)
for each incoming connection.
It listen on the standard 1337 port.
Created by Simone Kalb, Gian Maria Simbula
Copyright 2013 CRS4
eMail: [email protected]
eMail: [email protected]
*
*/
var fs = require('fs'),
http = require('http');
var xpMobileNode = require('xpMobileNode');
var mapIPMobileNode = new Object();
var id = 0;
var port = 1337;
http.createServer(function (req, res) {
if (req.url == '' || typeof(req.url) === undefined) {
/**
* Just handles the case whether no input file is specified
* You can then try different mask like index.php, index.js, and so on.
*/
console.log("[xpFileServer] No input file specified, using default\n");
req.url = '/index.html';
}
fs.readFile(__dirname + req.url, function (err,data) {
/**
* It just sends to the remote resource the file it asked
* and prints a message to the console.
*/
if (err) {
res.writeHead(404);
console.log("[xpFileServer] Unable to deliver document\n");
res.end(JSON.stringify(err));
return;
}
var node = new xpMobileNode(id, res);
node.init();
mapIPMobileNode[req.connection.remoteAddress] = node;
res.writeHead(200);
res.end(data);
});
id++;
}).listen(port);
console.log("Listening on port "+port);
/**
* Listen to specified port
*/