Web.js Node.js 》 Node.js  Web.js and
Web.js Node.js JavaScript on Server-side http://nodejs.org/
Web.js Node.js E vent A sync N onBlocking
Web.js Node.js 前 端们,回想下当年你做页面的时候,因为各种原因,使页面 Block 住的情景吧! 再回想下 Ajax 一直在等待服务器的回答,但是因为服务器突然挂掉,而使到正在使用中的页面没有任何动作的情景吧!
Web.js Node.js
Web.js Node.js var  http = require( 'http' ), fs = require( 'fs' ), url = require( 'url' ); var  server = http. createServer (function ( req ,  res ) { var  filePath = url. parse (req. url ). pathName ; fs. readFile (filePath, function ( err ,  data ) { res. writeHead ( 200 , { 'Content-type' :  'text/plain' }); res. write ( data ); res. end (); }); }); server. listen ( 8888 );
Web.js Node.js http://expressjs.com/
Web.js Node.js var  express   = require( 'express' ), app = express. createServer (); app. configure (function(){ app. use (express. static ( __dirname )); }); app. listen ( 8888 );
Web.js Node.js /:Web.js http://web.jit.im/
Web.js Node.js require( 'webjs' ). run ()
Web.js Node.js { 速度! 灵活度! 简易度!
Web.js Node.js } 兼容性 ! 稳定性 ! 还原性 !
Web.js 可以脱离 NPM(Node.js package manager) 安装, Web.js 不需要利用编译器进行编译,可以直接复制到应用目录使用。 Express 需要利用 NPM 调用 C++ 编译器进行编译,而且 C++ 部分并不是性能做优化,而只是为了生成一个 CLI(command-line interface) ,因为 Express 的语法复杂,开发者在每一个应用开始编写的时候要做的准备工作很多。 而 Web.js 的语法非常简单,不需要太多的记忆,完全是 JavaScript 的基础语法。 Web.js Node.js
var  express = require( 'express' ), app = express. createServer (); app. get ( '/:year/:mouth/:day/:id.jpg' , function ( req ,  res ) { res. sendFile (req. params . year  +  '-'  + req. params . mouth  +  '-'  + req. params . day  +  '-'  + req. params . id  +  '.jpg' ); }); app. get ( '/:action' , function (req, res) { var  action = req. params . action ; res. send ( 'Your action is '  + action); }); Web.js Node.js
var  web = require( 'webjs' ), urlRouter = { '/:year/:mouth/:day/:id.jpg' :  '$1-$2-$3-$4.jpg' }, getRouter = { '/:action' : function ( req ,  res ) { res. send ( 'Your action is '  + req. path . action ); } }; web. run ( urlRouter ,  8888 ) . get ( getRouter ); Web.js Node.js Web.js
Web.js Node.js Request Server POST GET PostRouter PostHandler UrlRouter GetRouter Response File System Redirect GetHandler
Web.js Node.js DEMO ——  Web.js TODOS http://todos.jit.im/
Web.js Node.js Node.js + MongoDB Web.js + Mongoose
Web.js Node.js var  web = require( 'webjs' ), mongoose = require( 'mongoose' ), config = require( './config' ); var  Item =  new  mongoose. Schema ({ title:  String , status:  Number }); mongoose. model ( 'Item' ,  Item ); Item = mongoose. model ( 'Item' ); var  getRouter = { 'getItems' : function ( req ,  res ) { Item. find ({}, function ( err ,  items ) { res. sendJSON ( items ); }); } }, postRouter = { 'addItem' : function ( req ,  res ) { new  Item({ ...... }). save (function ( err ) { ...... }); }, ...... }; web. run ({},  config . www . port ) . get ( getRouter ) . post ( postRouter );
Web.js Node.js 不会有哪个 SB 的 Node.js 第三方模块开发者会提供同步的 API !! 不过 Node.js 原生 API 里的确有。。 =_=
Web.js Node.js Web.js 的 API  是全裸的 Node.js 原生代码可以无缝迁移 //Node.js 官方 Hello World var  http = require( 'http' ); http. createServer (function ( req ,  res ) { res. writeHead ( 200 , { 'Content-Type' :  'text/plain' }); res. end ( 'Hello World\n' ); }). listen ( 1337 ,  "127.0.0.1" ); //Web.js var  web = require( 'webjs' ); web. run ({},  1337 ,  '127.0.0.1' ) . get ({ '/' : function (req, res) { res. end ( 'Hello World\n' ); //res.send('Hello World'); } });
Web.js Node.js 别小看 Node.js , Node.js 也是 CMS 潜力股! 上传文件 var  postRouter = { 'upload' : function ( req ,  res ) { fs. writeFile ( 'somefile.txt' , //file name req. data . upload , //file data function ( err ) { //callback if ( err ) return res. send ( 'Upload failed' ); res. send ( 'Upload successed' ); }); } } * 基于 node-formiable express 原生不支持文件上传
Web.js Node.js RESTful API
Web.js Node.js
Web.js Node.js Sencha Touch + Web.js MongoDB(Mongoose) | Node.js(Web.js) | JSONP | Front End (Sencha Touch)
Web.js Node.js var  mongoose = require( "mongoose" ), …… web = require( "webjs" ); …… var  Post =  new  mongoose. Schema ({ …… }); …… var  getRouter = { "jsonData" : function (req, res) { Post. find ({}, function ( err ,  posts ) { res. sendJSON (req. qs . callback  +  '('  + JSON. stringify ( posts ) +  ')' ); }) } }; web. run ({}, config. www . port , config. www . host ) . get (getRouter); Web.js + Mongoose
Web.js Node.js Ext. apply (mmq. views , { hotMsgInnerList: new mmq. views . HotMsgInnerList ({ store : new Ext. data . Store ({ id:  'store_hm' , model:  'mmq.models.HotMsg' , autoLoad:  true , proxy : { type:  'scripttag' , url:  'http://localhost:8888/jsonData' , reader: { type:  'json' , root:  'data' }, } }) }) });
EventProxy ( 朴灵  Jackson Tian) Jscex ( 老赵  Jeffrey) Backbone (Document Cloud) Underscore (Document Cloud) Mongoose (LearnBoost) MongoSkin ( 桂林 ) …… Web.js Node.js Friendly Modules
Web.js Node.js Q&A

Node.js and Web.js

  • 1.
    Web.js Node.js 》Node.js Web.js and
  • 2.
    Web.js Node.js JavaScripton Server-side http://nodejs.org/
  • 3.
    Web.js Node.js Event A sync N onBlocking
  • 4.
    Web.js Node.js 前端们,回想下当年你做页面的时候,因为各种原因,使页面 Block 住的情景吧! 再回想下 Ajax 一直在等待服务器的回答,但是因为服务器突然挂掉,而使到正在使用中的页面没有任何动作的情景吧!
  • 5.
  • 6.
    Web.js Node.js var http = require( 'http' ), fs = require( 'fs' ), url = require( 'url' ); var server = http. createServer (function ( req , res ) { var filePath = url. parse (req. url ). pathName ; fs. readFile (filePath, function ( err , data ) { res. writeHead ( 200 , { 'Content-type' : 'text/plain' }); res. write ( data ); res. end (); }); }); server. listen ( 8888 );
  • 7.
  • 8.
    Web.js Node.js var express = require( 'express' ), app = express. createServer (); app. configure (function(){ app. use (express. static ( __dirname )); }); app. listen ( 8888 );
  • 9.
    Web.js Node.js /:Web.jshttp://web.jit.im/
  • 10.
    Web.js Node.js require('webjs' ). run ()
  • 11.
    Web.js Node.js {速度! 灵活度! 简易度!
  • 12.
    Web.js Node.js }兼容性 ! 稳定性 ! 还原性 !
  • 13.
    Web.js 可以脱离 NPM(Node.jspackage manager) 安装, Web.js 不需要利用编译器进行编译,可以直接复制到应用目录使用。 Express 需要利用 NPM 调用 C++ 编译器进行编译,而且 C++ 部分并不是性能做优化,而只是为了生成一个 CLI(command-line interface) ,因为 Express 的语法复杂,开发者在每一个应用开始编写的时候要做的准备工作很多。 而 Web.js 的语法非常简单,不需要太多的记忆,完全是 JavaScript 的基础语法。 Web.js Node.js
  • 14.
    var express= require( 'express' ), app = express. createServer (); app. get ( '/:year/:mouth/:day/:id.jpg' , function ( req , res ) { res. sendFile (req. params . year + '-' + req. params . mouth + '-' + req. params . day + '-' + req. params . id + '.jpg' ); }); app. get ( '/:action' , function (req, res) { var action = req. params . action ; res. send ( 'Your action is ' + action); }); Web.js Node.js
  • 15.
    var web= require( 'webjs' ), urlRouter = { '/:year/:mouth/:day/:id.jpg' : '$1-$2-$3-$4.jpg' }, getRouter = { '/:action' : function ( req , res ) { res. send ( 'Your action is ' + req. path . action ); } }; web. run ( urlRouter , 8888 ) . get ( getRouter ); Web.js Node.js Web.js
  • 16.
    Web.js Node.js RequestServer POST GET PostRouter PostHandler UrlRouter GetRouter Response File System Redirect GetHandler
  • 17.
    Web.js Node.js DEMO—— Web.js TODOS http://todos.jit.im/
  • 18.
    Web.js Node.js Node.js+ MongoDB Web.js + Mongoose
  • 19.
    Web.js Node.js var web = require( 'webjs' ), mongoose = require( 'mongoose' ), config = require( './config' ); var Item = new mongoose. Schema ({ title: String , status: Number }); mongoose. model ( 'Item' , Item ); Item = mongoose. model ( 'Item' ); var getRouter = { 'getItems' : function ( req , res ) { Item. find ({}, function ( err , items ) { res. sendJSON ( items ); }); } }, postRouter = { 'addItem' : function ( req , res ) { new Item({ ...... }). save (function ( err ) { ...... }); }, ...... }; web. run ({}, config . www . port ) . get ( getRouter ) . post ( postRouter );
  • 20.
    Web.js Node.js 不会有哪个SB 的 Node.js 第三方模块开发者会提供同步的 API !! 不过 Node.js 原生 API 里的确有。。 =_=
  • 21.
    Web.js Node.js Web.js的 API 是全裸的 Node.js 原生代码可以无缝迁移 //Node.js 官方 Hello World var http = require( 'http' ); http. createServer (function ( req , res ) { res. writeHead ( 200 , { 'Content-Type' : 'text/plain' }); res. end ( 'Hello World\n' ); }). listen ( 1337 , "127.0.0.1" ); //Web.js var web = require( 'webjs' ); web. run ({}, 1337 , '127.0.0.1' ) . get ({ '/' : function (req, res) { res. end ( 'Hello World\n' ); //res.send('Hello World'); } });
  • 22.
    Web.js Node.js 别小看Node.js , Node.js 也是 CMS 潜力股! 上传文件 var postRouter = { 'upload' : function ( req , res ) { fs. writeFile ( 'somefile.txt' , //file name req. data . upload , //file data function ( err ) { //callback if ( err ) return res. send ( 'Upload failed' ); res. send ( 'Upload successed' ); }); } } * 基于 node-formiable express 原生不支持文件上传
  • 23.
  • 24.
  • 25.
    Web.js Node.js SenchaTouch + Web.js MongoDB(Mongoose) | Node.js(Web.js) | JSONP | Front End (Sencha Touch)
  • 26.
    Web.js Node.js var mongoose = require( "mongoose" ), …… web = require( "webjs" ); …… var Post = new mongoose. Schema ({ …… }); …… var getRouter = { "jsonData" : function (req, res) { Post. find ({}, function ( err , posts ) { res. sendJSON (req. qs . callback + '(' + JSON. stringify ( posts ) + ')' ); }) } }; web. run ({}, config. www . port , config. www . host ) . get (getRouter); Web.js + Mongoose
  • 27.
    Web.js Node.js Ext.apply (mmq. views , { hotMsgInnerList: new mmq. views . HotMsgInnerList ({ store : new Ext. data . Store ({ id: 'store_hm' , model: 'mmq.models.HotMsg' , autoLoad: true , proxy : { type: 'scripttag' , url: 'http://localhost:8888/jsonData' , reader: { type: 'json' , root: 'data' }, } }) }) });
  • 28.
    EventProxy ( 朴灵 Jackson Tian) Jscex ( 老赵 Jeffrey) Backbone (Document Cloud) Underscore (Document Cloud) Mongoose (LearnBoost) MongoSkin ( 桂林 ) …… Web.js Node.js Friendly Modules
  • 29.