Fork Me on GitHub

webjs

version 0.5.3

Simple and stable development for Node.js

webjs makes your websites' and web apps' server-side script simplier, faster, more stable and more readable.

Server

First, we need to run a new server. You can use the web controller or create app.

var web = require('webjs');
//Use the web controller
web.run()
   .use(web.static(__dirname));

//Create a app like connect
var app = web.create()
    //default create a http server.
    //you can set the first argument like 'https' or 'net'.
    .use(web.static(__dirname))
    .listen(8080);

Config

After run a server, we need to config it.

web.run()
   .use(web.static(__dirname + '/../static'))
   .config({
     'views': __dirname + '/../views',
     'view engine': 'jade',
     'readonly': false,
     'mode': 'dev'
   });

Router

Router is webjs's core. We can use it to build a true web app.

web.get({
  '/': function (req, res) {
    res.render('index', {
      title: 'Index',
      local: {
        name: 'webjs'
      }
    });
  },
  '/:name': function (req, res) {
    res.render('index', {
      title: 'Hello',
      local: {
        name: req.param.name
      }
    });
  }
});

CLI (Command Line Interface)

webjs support a simple cli to create a mvc web app.

$ mkdir myapp
$ cd myapp
$ webjs -h
$ webjs init --all --port 8888

Also you can use the cli to run a simple static server on current directory.

$ cd sencha-touch-2
$ webjs run 80

This is full usage of the cli.

Commands:
  run <port>
  run a simple http server here

  init
  Initialize a webjs app here

Options:
  -h, --help           output usage information
  -V, --version        output the version number
  -c, --cookie         Add cookie support
  -s, --session        Add session support
  -b, --body           Add body parser support
  --compress           Add compress support
  --compiler <engine>  Add compiler support
  -r, --rest           Add more HTTP methods support
  --mustache           Add mustache template engine support (defaults to jade)
  --ejs                Add ejs template engine support (defaults to jade)
  --tmpl <engine>      Set the template engine
  --mode <mode>        Set the mode (defaults to production)
  --port <port>        Set the port (defaults to 80)
  -a --all             Enable all functions

Examples:
  $ webjs
  $ webjs init -s -c -b -r --mode pro --port 8888 --compress --compiler less
  $ webjs init --all
  $ webjs run 8080

Installation

You can install webjs from npm (Node.js Package Manager).

$ npm install webjs
$ webjs -v
v0.5.3

Or you can install the latest version.

$ npm install webjs@latest

Programming

Congratulations! webjs use the native JavaScript syntax, so you do not need to learn any special syntax.

Response any data

webjs support some extra response methods to make the program send data easier.

Plain String

res.send('Sending a string');
//You can set the second param as a MIME type string, like 'text/css'.

JSON Object

res.sendJSON({
  name: 'Will Wen Gunn',
  age: 16,
  contury: 'China',
  languages: ['Chinese', 'Cantonese', 'English', 'Janpanse']
});

JSONP (JSON with Padding)

webjs will get the callback function from the request.

res.sendJSONP({
  name: 'Will Wen Gunn',
  age: 16,
  contury: 'China',
  languages: ['Chinese', 'Cantonese', 'English', 'Janpanse']
});

Send a file

res.sendFile(__dirname + '/test.js')

Send a error

You can send a http error to client, suck as 404, 500, 503.

res.sendError(404);
//Just need to set a number.

Template render

webjs will use the template engine that you set to web controller or the server.

web.set('view engine', 'jade');
......
res.render('hello', { name: 'Will Wen Gunn' });

webjs default will create a layout.jade, if you do not need it, you can delete it or do this.

res.render('hello', {
  name: 'Will Wen Gunn',
  layout: false
});

Response pipelining

Response pipelining is one of the core of webjs's too. It can be used to compress, compile, data collect, analytics and so on. It will be done before the data be sended to clients.

Compress

var zlib = require('zlib');
web.use(function (req, res, next) {
    res.pipelining(function () {
        var acceptEncoding = req.headers['accept-encoding'] || "";
        var need = /css|js|html/ig.test(res.format);
        if (need && acceptEncoding.match(/\bgzip\b/)) {
            res.header('Content-Encoding', 'gzip');
            res.removeHeader('Content-Length');
            return zlib.createGzip();
        } else if (need && acceptEncoding.match(/\bdeflate\b/)) {
            res.header('Content-Encoding', 'deflate');
            res.removeHeader('Content-Length');
            return zlib.createDeflate();
        }
    });
    next();
});

File complier

var less = require('less');
//Using dataStream https://github.com/iwillwen/node-dataStream
var dataStream = require('dataStream');

web.use(function (req, res, next) {
  res.pipelining(function () {
    //Check the extname
    if (res.format !== 'less') {
      return;
    } else {
      //Make the stream do not pass on the data(unreadable).
      var complieStream = new dataStream({ readable: false });
      complieStream.on('complate', function () {
        //Reviced all data from the File System
        var body = complieStream.body().toString();
        //complieStream.body() will return a Buffer object
        less.render(body, { path: __dirname }, function (err, css) {
          if (err) return complieStream.emit('data', body);
          res.header('Content-Length', css.length);
          res.header('Content-Type', 'text/css');
          complieStream.emit('data', css);
        });
      });
    }
  });
});

This page is still working.

About

webjs by Will Wen Gunn

Released under the MIT license.


This page designed By Alexis Sellier