Skip to content

Commit 2c05d3d

Browse files
committed
Added node.js static file server to help people see the example in
action easier
1 parent 04cf9f6 commit 2c05d3d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ define(["jquery", "jquery.alpha", "jquery.beta"], function($) {
6464

6565
###How to see it in action
6666

67-
Just serve up the www/ folder using any web server you'd like.
67+
Just serve up the www/ folder using any web server you'd like. To get you set up quickly, we've included a node.js static file server in tools/. Start the server by typing `node tools/server.js` from the command line, and then go to [localhost:8888/www/app.html](http://localhost:8888/www/app.html) in your browser.
6868

6969
###How to optimize ut using r.js
7070
To use the optimizer, you need [node.js](http://nodejs.org) or Java 6 installed. These instructions assume Node is being used. See the [Optimization page](http://requirejs.org/docs/optimization.html) for more information.

tools/server.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* node static file server:
3+
* modified from https://gist.github.com/rpflorence/701407
4+
*/
5+
/*jslint evil: true, nomen: true, sloppy: true */
6+
7+
var http = require('http'),
8+
url = require('url'),
9+
path = require('path'),
10+
fs = require('fs'),
11+
port = process.argv[2] || 8888,
12+
types = {
13+
'html': 'text/html',
14+
'js': 'application/javascript'
15+
};
16+
17+
http.createServer(function (request, response) {
18+
var uri = url.parse(request.url).pathname,
19+
filename = path.join(__dirname, '..', uri);
20+
21+
fs.exists(filename, function (exists) {
22+
if (!exists) {
23+
response.writeHead(404, {'Content-Type': 'text/plain'});
24+
response.write('404 Not Found\n');
25+
response.end();
26+
return;
27+
}
28+
29+
var type = filename.split('.');
30+
type = type[type.length - 1];
31+
32+
response.writeHead(200, { 'Content-Type': types[type] + '; charset=utf-8' });
33+
fs.createReadStream(filename).pipe(response);
34+
});
35+
}).listen(parseInt(port, 10));
36+
37+
console.log('Static file server running at\n => http://localhost:' + port + '/\nCTRL + C to shutdown');

0 commit comments

Comments
 (0)