Difference between revisions of "Web development/model-router"

From ISoft Wiki
Jump to: navigation, search
(First stuff)
(No difference)

Revision as of 16:58, 19 February 2013

model-router does a few things that make writing data servers easier:

  • Routing
  • Session handling/state
  • Wrapping up authentication
  • Checking for parameters existence

Including

You could have multiple router-things if you wanted to - you know, in case you were running multiple sites from the same node.js server, or something - but for your purposes you'll probably just want to instantiate one.

var Router = require('model-router')
var router = new Router()

It doesn't launch any servers of its own, that would be rude. You'll want to create your own server in the usual way, and then tell model-router to handle it if appropriate.

model-router only sends back JSON responses, and doesn't do anything with static content out of the box, so if you want to serve HTML/JavaScript files or something, you'll have to do it yourself:

require('http').createServer(function(req, res) {
	var pathname = require('url').parse(req.url).pathname
	var match
	if (match = /^\/static(\/.*)$/.exec(pathname)) {
		require('send')(req, match[1]).root('./static/').pipe(res)
	} else {
		router.handleRequest(req, res)
	}
}).listen(8080)