鲍勃の部屋
  • Blog
  • Work
  • About
  • Contact

Express 之Hello world -- 第1课

7/3/2014

0 Comments

 
    Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.


官网地址

下面我们开始 express 的 hello world 之旅 (确保你已经安装了nodeJS哟 )

第一步,任意随意创建一个目录,比如叫 hello-express 

然后新建一个文件叫 package.json   (express 的版本号可通过 npm info express version 获取)
{
  "name": "hello-express",
  "description": "hello world test app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "4.4.5"
  }
}

Read More
0 Comments

和鲍勃一起来学Node.js--第13课

6/19/2014

0 Comments

 
书接上回。

我们已经实现了上传图片,那么能不能直接在浏览器中显示我们上传的图片呢?

答案是:能!

在requestHandler.js 里的 upload() 函数中增加一个图像就行了
response.write('<img src="/show_upload" />');
如你所想,接下来要增加一个新的路由,使得show_upload读取最新上传的图片内容,并返回
function show_upload(response, request){
    console.log("show_upload: "+pic_full_name);
    fs.readFile(pic_full_name, "binary", function(error, file){
        if(error){
            response:writeHead(500, {"Content-Type":"text/plain"});
            response.write(error+"\n");
            response.end();
        }else{
            response.writeHead(200, {"Content-Type":"image/png"});
            response.write(file, "binary");
            response.end();
        }
    });
}

exports.a = a;
exports.b = b;
exports.upload = upload;
exports.show_upload = show_upload;

Read More
0 Comments

和鲍勃一起来学Node.js--第12课

6/19/2014

0 Comments

 
本课学习如果上传文件,并把图在浏览器中显示出来。

这个用例在90年代完全可以满足用于IPO的商业模型了,今天我们通过它可以学习两件事情:

1. 如何安装外部node.js模块
2. 如何将它们应用到我们的应用中。

这里,我们要用到的模块是 Felix Geisendorfer开发的 node-formidable模块,它对解析上传的文件数据做了很好的抽象。

如何安装模块呢? node.js有自己的包管理器叫NPM.  

看过之前 PhoneGap 之  HelloWorld  的朋友应该会不陌生。

通过下一条命令完成 formidable 的安装
npm install formidable

终端输出如下:

npm http GET https://registry.npmjs.org/formidable
npm http 200 https://registry.npmjs.org/formidable
npm http GET https://registry.npmjs.org/formidable/-/formidable-1.0.15.tgz
npm http 200 https://registry.npmjs.org/formidable/-/formidable-1.0.15.tgz
formidable@1.0.15 node_modules\formidable

说明模块已经安装成功了

查看执行npm install 的目录,会发现多了一个 node_modules/formidable 文件夹

Read More
0 Comments

和鲍勃一起来学Node.js--第11课

6/17/2014

0 Comments

 
本课教你如何处理post请求

通过前面10节课的学习,你已经是新手中的专家了, 你会想到采用异步回调来实现非阻塞地处理post请求的数据。

这里采用非阻塞的处理方式是明智的,因为一般post请求都比较“重”, 用户可能会输入大量的内容。

为了使整个过程非阻塞,node.js 会将post数据拆分成很多小的数据块,然后通过触发特定的事件,将这些小数据块传递给回调函数。 这里的特定事件有data事件(表示新的小数据块到了) 还有 end事件(表示 所有的数据都已经接收完毕)

我们需要告诉node.js当这些事件触发的时候,回调哪些函数。

怎么告诉呢,通过在request对象上注册监听器(listener)来实现。 如下所示
request.addListener("data", function(chunk){
        //called when a new chunk of data was received
});

request.addlistener("end", function(){
        // called when all chunks of data have been received
});

Read More
0 Comments

和鲍勃一起来学Node.js--第10课

6/17/2014

0 Comments

 
本课教你如何肥预先写好的html内容展示到浏览器中

之前所有课程中,浏览器中看到的内容都是程序生成的, 能不能直接读取写好的文件内容展示出来呢

答案是使用fs模块。

首先, 创建一个文件 html/form.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>

<body>
<form action="/upload" method="post">
<textarea name="text" rows="20" cols="60"></textarea>
<input value="Submit Text" />
</form>
</body>
</html>

Read More
0 Comments

和鲍勃一起来学Node.js--第09课

6/15/2014

0 Comments

 
好,我们马上开始介绍,如何以正确的方式,把耗时又不阻塞的内容让浏览器作出响应

到现在为止,我们的代码已经可以通过各个模块之间传递返回值

requestHandler->routes->server

最后在server中通过 response.write 回写到浏览器中

现在我们改一下,不再传递字符串的返回值,而是直接传递response对象, 把response对象,从server->routes->requesthanler传下去。

首先是server.js, 把response, 向下传递给routes, 同时删除所有的response返回,交给下面的requestHanler处理。
function start(get, config){
var http = require("http");
var url = require("url");


var server = http.createServer(function(request, response){
var pathname = url.parse(request.url).pathname;
get(pathname, config, response);

});
server.listen(8888);
}

exports.start = start;


Read More
0 Comments

和鲍勃一起来学Node.js--第8课

6/15/2014

0 Comments

 
阻塞?非阻塞?

到目前为止,我们的路由模块工作一切正常,但实际上在处理阻塞讲求时就会出现问题。

我们自底向上的稍微修改一下各个模块

第一个是 requesthandler.js, 在路由b的时候增加一个 睡眠 5秒
function a(){
console.log("welcome to visit a :) ");
return "This is A!"
}
function b(){
sleep(5000);
return "Hello B";

}
function sleep(t){
var now = new Date();
var d = now.getTime();
var e = d + t;

while(new Date().getTime() < e);
}

exports.a = a;
exports.b = b;

Read More
0 Comments

和鲍勃一起来学Node.js--第7课

6/12/2014

0 Comments

 
本课我们来创建路由处理模块

上一课,我们已经学会了怎么样去创建路由模块, 那么要真正的实现路由,还必须要添加具体的处理逻辑。

那么在哪里添加呢?

在routes.js 里的 get函数里添加下面的代码来处理

if (pathname == '/home') {
        //do something
} else if (pathname == '/about') {
        // do something

}

这样子,功能上是没问题的。不过并不是一个好的方案。为什么呢?

因为,上一课我们实现的路由模块,虽然简单,但是很纯粹。 它只负责实现路由的过程。

路由结束之后,具体采取什么行动,它并不负责

如果把路由结束后的 具体行为 也写入到routes模块,那么, 当有成千上万个路由请求时, 将无法很好的扩展routes模块。

而且,如果routes里实现了具体的处理逻辑,那么这个模块也就只能成为一个具体的服务器的模块,而不再是一个可重用的,通用用的routes模块了。
(假如,我又要实现一个新的服务器server2.js,  /home 的逻辑是完成不一样的,那么直接把routes.js拿来使用显示不能得到正确的结果)

因此,我们应该针对 路由结束后的具体行为编写一个单独的模块, 在此把它称为requestHandler模块,在模块中,为每一个请求处理 ,添加一个对应的函数,最后把这些函数作为模块的方法导出:

编写模块我们已经很熟悉了, 直接上代码

Read More
0 Comments

和鲍勃一起来学Node.js--第6课

6/12/2014

0 Comments

 
本课教你创建 服务器的 路由功能

如果你以前用过其它语言的框架,应该对路由不会陌生

比如 PHP的 Laravel里专门有个routes.php用来配置路由信息,形如:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
return View::make('hello');
});

Route::get ('DZGame/ID101/{operation}', 'DZGame\ID101\Controller@main');

Read More
0 Comments

和鲍勃一起来学Node.js--第5课

6/10/2014

0 Comments

 
Picture
这一课 简单学习一下 node自带的url模块  和 querystring模块


打开我们前面的 server.js 文件, 添加下面绿色显示的代码

function start(){
var http = require("http");
var url = require("url");
var querystring = require("querystring");

var server = http.createServer(function(requese, respnose){
response.writeHead(200, {"Content-Type":"text/plain"});
var url_obj = url.parse(request.url);
response.write("\n pathname:"+url_obj.pathname);
response.write("\n query:"+url_obj.query);

var qs = querystring.parse(url_obj.query);
response.write("\n a:"+qs["a"]);
response.write("\n b:"+qs.b);
response.write("\n c:"+qs.c);

response.end();
});
server.listen(8888);
}

exports.start = start;


照旧运行 node index.js 

浏览器中访问 http://localhost:8888/abcd?c=bob&b=joke&a=8 

看到


Read More
0 Comments
<<Previous

    Author

    鲍勃 (上海)
    互联网码农

    小A

    Archives

    July 2014
    June 2014
    May 2014

    Categories

    All
    AngularJS
    App
    Bootstrap
    Cocos2dx-c++
    Css
    CSS3
    Design Pattern
    Express
    Funny
    Game
    Html5
    Jasmine
    Javascript
    Lua
    Music
    Node.js
    Pattern
    Phonegap
    Php
    Python
    Tdd
    Tips

    RSS Feed

Powered by Create your own unique website with customizable templates.