Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added example.js and bulked out the readme a bit

  • Loading branch information...
commit 312ff2a46f6be2196368438b338e853f33b0f322 1 parent cba86e0
Simon Willison authored
Showing with 52 additions and 5 deletions.
  1. +19 −5 README.txt
  2. +32 −0 example.js
  3. +1 −0  static-demo/hello.txt
24 README.txt
View
@@ -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
View
@@ -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
View
@@ -0,0 +1 @@
+Hello from a static file.
Please sign in to comment.
Something went wrong with that request. Please try again.