NodeJS Web Framework

Express

Route

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('hello world');
});

express.Router

var express = require('express');
var router = express.Router();

// define the about route
router.get('/about', function(req, res) {
  res.send('About birds');
});

module.exports = router;
var birds = require('./birds');
...
app.use('/birds', birds);

birds.js

app.js

middleware

An Express application is essentially a series of middleware calls.

Middleware can:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.

middleware

var app = express();

// a middleware with no mount path; gets executed for 
// every request to the app
app.use(function (req, res, next) {
  console.log('Time:', Date.now());
  next();
});

// a middleware mounted on /user/:id; will be executed for 
// any type of HTTP request to /user/:id
app.use('/user/:id', function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

// a route and its handler function (middleware system) which 
// handles GET requests to /user/:id
app.get('/user/:id', function (req, res, next) {
  res.send('USER');
});
var app = express();
var router = express.Router();

// a middleware with no mount path, gets executed for every request to the router
router.use(function (req, res, next) {
  console.log('Time:', Date.now());
  next();
});

// a middleware sub-stack shows request info for any type of HTTP request to /user/:id
router.use('/user/:id', function(req, res, next) {
  console.log('Request URL:', req.originalUrl);
  next();
}, function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

// handler for /user/:id which renders a special page
router.get('/user/:id', function (req, res, next) {
  console.log(req.params.id);
  res.render('special');
});

// mount the router on the app
app.use('/', router);
app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
var options = {
  dotfiles: 'ignore',
  etag: false,
  extensions: ['htm', 'html'],
  index: false,
  maxAge: '1d',
  redirect: false,
  setHeaders: function (res, path, stat) {
    res.set('x-timestamp', Date.now());
  }
}

app.use(express.static('public', options));
$ npm install mysql
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'dbuser',
  password : 's3kreee7'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ', rows[0].solution);
});

connection.end();

DEMO

$ npm install express-generator -g
$ express myapp
$ cd myapp
$ npm install
$ DEBUG=myapp:* npm start

Koa

Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. 

middleware

Generator function

var compute = function* (a, b) {
  var sum = a + b;
  console.log(sum);
};

var generator = compute(4, 2);
generator.next();

context

app.use(function *(){
  this; // is the Context
  this.request; // is a koa Request
  this.response; // is a koa Response
  this.req; // is a node Request
  this.res; // is a node Response
});

route

var app = require('koa')();
var router = require('koa-router')();

router.get('/', function *(next) {...});

app.use(router.routes());

reference

Express VS Koa

Features Koa Express
Middleware Kernel
Routing
Templating
Sending Files
JSONP
  • Meteor

  • LoopBack

  • Sails

  • Hapi

  • Restify

END

NodeJS Web Framework

By xzhang

NodeJS Web Framework

  • 891