|
1 | 1 | var http = require('http'), |
2 | | - fs = require('fs'), |
3 | | - path = require('path'), |
4 | | - director = require('../../../lib/director'), |
5 | | - index; |
| 2 | + fs = require('fs'), |
| 3 | + path = require('path'), |
| 4 | + director = require('../../../lib/director'), |
| 5 | + index; |
6 | 6 |
|
7 | 7 | fs.readFile(path.join(__dirname, '..', 'html5-routes-harness.html'), function (err, data) { |
8 | | - if (err) { |
9 | | - throw err; |
10 | | - } |
| 8 | + if (err) { |
| 9 | + throw err; |
| 10 | + } |
11 | 11 |
|
12 | | - index = data; |
| 12 | + index = data; |
13 | 13 | }); |
14 | 14 |
|
15 | 15 | var CONTENT_TYPES = { |
16 | | - '.js' : 'text/javascript', |
17 | | - '.css' : 'text/css' |
| 16 | + '.js' : 'text/javascript', |
| 17 | + '.css' : 'text/css' |
18 | 18 | }; |
19 | 19 |
|
| 20 | +var dirname = path.dirname(module.uri); |
| 21 | + |
20 | 22 | // Dummy file server |
21 | 23 | function fileServer(folder, file) { |
22 | | - var root = path.resolve(__dirname, '..'); |
23 | | - if (folder === 'build' || folder === 'node_modules') { |
24 | | - root = path.resolve(root, '..', '..'); |
25 | | - } |
| 24 | + var root = path.resolve(dirname, '..'); |
| 25 | + |
| 26 | + if (folder === 'build' || folder === 'node_modules') { |
| 27 | + root = path.resolve(root, '..', '..'); |
| 28 | + } |
| 29 | + |
| 30 | + if (file === undefined) { |
| 31 | + file = folder; |
| 32 | + folder = '.'; |
| 33 | + } |
26 | 34 |
|
27 | | - var filepath = path.resolve(root, folder, file); |
| 35 | + var filepath = path.resolve(root, folder, file); |
28 | 36 |
|
29 | | - var res = this.res; |
| 37 | + var res = this.res; |
30 | 38 |
|
31 | | - (fs.exists || path.exists)(filepath, function (exists) { |
32 | | - if (exists) { |
33 | | - fs.readFile(filepath, function (err, data) { |
34 | | - if (err) { |
35 | | - res.writeHead(404); |
36 | | - res.end(); |
37 | | - } |
| 39 | + (fs.exists || path.exists)(filepath, function (exists) { |
| 40 | + if (exists) { |
| 41 | + fs.readFile(filepath, function (err, data) { |
| 42 | + if (err) { |
| 43 | + res.writeHead(404); |
| 44 | + res.end(); |
| 45 | + } |
38 | 46 |
|
39 | | - res.writeHead(200, {'Content-Type': CONTENT_TYPES[path.extname(filepath)]}); |
40 | | - res.end(data); |
41 | | - }); |
42 | | - } else { |
43 | | - res.writeHead(404); |
44 | | - res.end(); |
45 | | - } |
46 | | - }); |
| 47 | + res.writeHead(200, {'Content-Type': CONTENT_TYPES[path.extname(filepath)]}); |
| 48 | + res.end(data); |
| 49 | + }); |
| 50 | + } else { |
| 51 | + res.writeHead(404); |
| 52 | + res.end(); |
| 53 | + } |
| 54 | + }); |
47 | 55 | } |
48 | 56 |
|
49 | 57 | var router = new director.http.Router({ |
50 | | - '/files': { |
51 | | - '/:folder': { |
52 | | - '/(.+)': { |
53 | | - get: fileServer |
54 | | - }, |
| 58 | + '/files': { |
| 59 | + '/:folder': { |
| 60 | + '/(.+)': { |
| 61 | + get: fileServer |
| 62 | + }, |
55 | 63 |
|
56 | | - get: fileServer |
57 | | - } |
58 | | - } |
| 64 | + get: fileServer |
| 65 | + } |
| 66 | + } |
59 | 67 | }); |
60 | 68 |
|
61 | 69 | var server = http.createServer(function (req, res) { |
62 | | - router.dispatch(req, res, function (err) { |
63 | | - if (err && req.url != '/favicon.ico') { |
64 | | - // By default just reply with the index page |
65 | | - this.res.writeHead(200, {'Content-Type': 'text/html'}); |
66 | | - this.res.end(index); |
67 | | - } |
68 | | - }); |
| 70 | + router.dispatch(req, res, function (err) { |
| 71 | + if (err && req.url !== '/favicon.ico') { |
| 72 | + // By default just reply with the index page |
| 73 | + this.res.writeHead(200, {'Content-Type': 'text/html'}); |
| 74 | + this.res.end(index); |
| 75 | + } |
| 76 | + }); |
69 | 77 | }); |
70 | 78 |
|
71 | 79 | server.listen(8080); |
0 commit comments