Added example.js and bulked out the readme a bit

This commit is contained in:
Simon Willison 2009-11-20 10:06:16 +00:00
Родитель cba86e0a8a
Коммит 312ff2a46f
3 изменённых файлов: 52 добавлений и 5 удалений

Просмотреть файл

@ -1,10 +1,24 @@
djangode
========
Utility functions for node.js that imitate some of the useful concepts from
Django.
Utility functions for node.js that imitate some useful concepts from Django.
http://nodejs.org/
http://www.djangoproject.com/
http://nodejs.org/
http://www.djangoproject.com/
var dj = require('./djangode');
Example usage:
var dj = require('./djangode');
dj.serve(dj.makeApp([
['^/$', function(req, res) {
dj.respond(res, '<h1>Homepage</h1>');
}],
['^/other$', function(req, res) {
dj.respond(res, '<h1>Other page</h1>');
}],
['^/page/(\\d+)$', function(req, res, page) {
dj.respond(res, '<h1>Page ' + page + '</h1>');
}]
]), 8008); // Serves on port 8008
Run "node example.js" for a slightly more interesting example.

32
example.js Normal file
Просмотреть файл

@ -0,0 +1,32 @@
var dj = require('./djangode');
var app = dj.makeApp([
['^/$', function(req, res) {
dj.respond(res, '<h1>djangode demo</h1> \
<ul> \
<li><a href="/delayed/1000">1 second delay page</a></li> \
<li><a href="/error">Error page with stacktrace</a></li> \
<li><a href="/404">Default 404</a></li> \
<li><a href="/redirect">Redirect back to /</a></li> \
<li><a href="/static/hello.txt">Static hello.txt</a></li> \
</ul> \
');
}],
['^/delayed/(\\d+)$', function(req, res, howlong) {
setTimeout(function() {
dj.respond(res, 'I delayed for ' + howlong);
}, parseInt(howlong, 10));
}],
['^/error$', function(req, res) {
"bob"("not a function"); // Demonstrates stacktrace page
}],
['^/redirect$', function(req, res) {
dj.redirect(res, '/');
}],
['^/favicon\.ico$', function(req, res) {
dj.respond(res, 'Nothing to see here');
}],
['^/(static-demo/.*)$', dj.serveFile] // Serve files from static-demo/
]);
dj.serve(app, 8009);

1
static-demo/hello.txt Normal file
Просмотреть файл

@ -0,0 +1 @@
Hello from a static file.