Node.js 入门(2)

< ![CDATA[

1.http 请求

//调用Node.js自带的http模块
var http = require("http");

//调用http模块提供的函数createServer
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("nodejs test");
    response.end();
}).listen(3000);
//监听3000端口

这个是上篇文章,提供的一个实例.

1.它和asp.net 或者php不一样.需要一个宿主,比如iis,才能 呈现到浏览器

.Node.js 是直接把内容呈现到浏览器.

2.首先,它还可以这样写

//调用Node.js自带的http模块
var http = require("http");

//调用http模块提供的函数createServer
http.createServer(onRequest).listen(3000);
//监听3000端口

function onRequest (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("nodejs test");
    response.end();
}
createServer()把onRequest当做方法传入参数中.
这样写也是完全正确的,类似匿名函数
3. 我们给莫个方法传递了一个函数,这个方法在有相对应的事件发生的时候,调用这个函数进行回调.
onRequest就是回调函数
Node.js是属于事件驱动.属于异步
比如
var http = require("http");
http.createServer(onRequest).listen(3000);
function onRequest (request, response) {
    console.log("有访问.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("nodejs test");
    response.end();
}
console.log("服务开启.");

 执行顺序应该是,下面的先输出.然后你刷新页面,请求onRequest方法.再输出上面的 ,看图

Node.js 入门(2)_第1张图片

参数,
//当函数触发的时候,这2个参数,就是一个是请求,一个是相应请求.
//跟.net里面的request.和response 性质一样,下面只是用了response输出,
http.createServer(function(request, response) {

 

模块

var http = require("http");

Node.js 把http封装起来.变成一个模块

现在新建一个index的js.把刚才代码复制进去.

now.index.js内容如下

var http = require("http");

 function index(){
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("nodejs ddtest");
    response.end();
}).listen(3000);
 }
exports.start =index;

 exports.start  就是,把这个index的方法暴漏出去. 

然后我们在原来的js里面 这样调用

   var index= require("./index");
   index.start();

这个跟调用Node.js 的http模块一致.

刷新下浏览器.工作正常.

 

如何来获得请求的“路由”

1.进行扩充下这个代码,引入url模块
var http = require("http");
var url = require("url");

function index(route){
        http.createServer(function(request, response) {
        var pathname=url.parse(request.url).pathname;
        console.log(pathname +"访问");
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("nodejs test");
        response.end();
    }).listen(3000);
     console.log("服务开启");
 }
exports.start =index;

浏览器. http://localhost:3000/abc 

就会打印出 

/abc访问

这个是一个简单的获取url路由的简单demo

扩展路由.

在JavaScript中,对象就是一个键/值对的集合 -- 你可以把JavaScript的对象想象成一个键为字符串类型的字典。

新建一个requestHandlers的模块.

然后这里面有各个路由执行的方法

比如

function start() {
    console.log("start");
}
function upload() {
    console.log("upload");
}
exports.start = start;
exports.upload = upload;

 然后引用它,

 

var requestHandler = require("./requestHandlers")

//  你可以把JavaScript的对象想象成一个键为字符串类型的字典。
   var handle={}
   handle["/"] = requestHandler.start;
   handle["/start"]=requestHandler.start;
   handle["/upload"]=requestHandler.upload;

 router 更改为

function route(handle,pathname)
{
    console.log(pathname+"router 访问");

    if(typeof handle[pathname]=="function")
    {
        handle[pathname]();
    } else
    {
      console.log("no request handler found for" +pathname);
    }
}

exports.router = route;

 这样的话.执行/upload.就到requesthandler里面方法了.

 

 

 

我写不下去了..还是看原文吧..

 参考:http://www.nodebeginner.org/index-zh-cn.html



 

]]>

你可能感兴趣的:(node.js)