This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo-music.js
48 lines (47 loc) · 1.56 KB
/
demo-music.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
/**
* Code found at http://blog.rassemblr.com/2011/04/a-working-static-file-server-using-node-js/
*/
var nodePath = require('path'),
nodeHttp = require("http"),
nodeFs = require('fs'),
nodeUrl = require("url"),
mime = require('mime'),
path = "./demo-music/",
port = 9090;
nodeHttp.createServer(function (request, response) {
var uri = nodeUrl.parse(request.url).pathname;
var filename = nodePath.join(path, uri);
nodeFs.exists(filename, function (exists) {
if (!exists) {
response.writeHead(404, {
"Access-Control-Allow-Origin": "*",
"Content-Type": "text/plain"
});
response.write("404 Not Found\n");
response.end();
return;
}
if (nodeFs.statSync(filename).isDirectory()) {
filename += 'index.html';
}
nodeFs.readFile(filename, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
"Access-Control-Allow-Origin": "*",
"Content-Type": "text/plain"
});
response.write(err + "\n");
response.end();
return;
}
var type = mime.lookup(filename);
var header = {
"Content-Type": type
};
response.writeHead(200, header);
response.write(file, "binary");
response.end();
});
});
}).listen(port);
console.log('Demo application v1 running at http://localhost:9090/');