Giter Site home page Giter Site logo

nerve's Introduction

Nerve

A microframework for node.js.

Features

  • Simple array-based DSL for request routing
  • Regular expression route matching, including passing of captured groups to route handler
  • Simple cookie and session support

Examples

Hello World

This Hello World app listens for http requests on port 8000:

var nerve = require('./nerve');

var hello = [
	["/", function(req, res) {
		res.respond("Hello, World!");
	}]
];

nerve.create(hello).listen(8000);

Nodewiki

Nodewiki is a tiny wiki built using Nerve and the redis-node-client.

Templates

The template.node.js project includes a sample application that shows how Nerve can be used to build a web application with templates.

Sample Application

This sample application makes use of Nerve's regular-expression URI path matching to pass a "name" parameter from the URI into a handler function. This can be extended to any number of named arguments in the handler function.

It also makes use of request method matching. The first matcher will only match get requests; the second will match any request method.

The application stores the user's name in the session, so that it can be used in subsequent responses.

var posix = require("./posix");
var nerve = require("./nerve"),
  get = nerve.get;

// define an application using request matcher/handler pairs
var app = [

	// this handler will only respond to GET requests
	[get(/^\/hello\/(\w+)$/), function(req, res, name) {
	
		// the session is available on every request; it currently
		// lasts for the browser session, but will soon be configurable.
		req.session["name"] = name;
	
		// respond takes a string and provides sensible defaults:
		// Content-Type: text/html, Content-Length: string length
		res.respond("Hello, " + name + "!");
	
	}],

	// this handler will respond to any request method
	[/^\/goodbye$/, function(req, res) {
	
		var name = req.session["name"];
		var message = "Goodbye, " + (name || "I hardly knew thee") + "!";

		// respond takes an object specifying content and headers,
		// and uses sensible defaults if not supplied
		res.respond({content: message, headers: {"Content-Type": "text/plain"}});
	
	}],

    ['/abc/:username/:post_id', function (req, res, params) {

        // params = { username: string, post_id: string }

    }]

];

// create and serve the application with 10 second session duration
nerve.create(app, {session_duration: 10000}).listen(8000);

nerve's People

Contributors

gjritter avatar jankuca avatar morganrallen avatar nikhilm avatar

Stargazers

JT Hamrick avatar

Watchers

James Cloos avatar JT Hamrick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.