1 | var http = require('http'),
|
2 | url = require('url'),
|
3 | path = require('path'),
|
4 | fs = require('fs'),
|
5 | port = process.argv[2] || 8888;
|
6 |
|
7 | http.createServer(function(request, response) {
|
8 |
|
9 | var uri = url.parse(request.url).pathname,
|
10 | filename = path.join(process.cwd(), uri);
|
11 |
|
12 | if (uri === '/images.json') {
|
13 |
|
14 | var json = require(path.join(process.cwd(), '../src', 'images.json'));
|
15 |
|
16 | response.writeHead(200);
|
17 | response.write(JSON.stringify(json));
|
18 | response.end();
|
19 |
|
20 | } else {
|
21 |
|
22 | fs.exists(filename, function(exists) {
|
23 |
|
24 | if(!exists) {
|
25 | response.writeHead(404, {'Content-Type': 'text/plain'});
|
26 | response.write('404 Not Found\n');
|
27 | response.end();
|
28 | return;
|
29 | }
|
30 |
|
31 | if (fs.statSync(filename).isDirectory()) filename += '/index.html';
|
32 |
|
33 | fs.readFile(filename, 'binary', function(err, file) {
|
34 | if(err) {
|
35 | response.writeHead(500, {'Content-Type': 'text/plain'});
|
36 | response.write(err + '\n');
|
37 | response.end();
|
38 | return;
|
39 | }
|
40 |
|
41 | response.writeHead(200);
|
42 | response.write(file, 'binary');
|
43 | response.end();
|
44 | });
|
45 | });
|
46 |
|
47 | }
|
48 |
|
49 | }).listen(parseInt(port, 10));
|
50 |
|
51 | console.log('Static file server running at\n => http://localhost:' + port + '/\nCTRL + C to shutdown'); |
\ | No newline at end of file |