node.js http server和client

server.jshtml

/*
var http = require("http");
var url = require("url");
var util = require("util");

http.createServer(function(req, res){
    res.writeHead(200, {"Content-Type":"text/plain; charset=utf-8"});
    //res.end(util.inspect(url.parse(req.url, true)));

    //解析 url 参数
    var params = url.parse(req.url, true).query;
    res.write("网站名:" + params.name);
    res.write("\n");
    res.write("网站 URL: " + params.url);
    res.end();
}).listen(3000);*/

/*
var http = require("http");
var querystring = require("querystring");

http.createServer(function(req, res){
   //定义了一个post变量,用于暂存请求体的信息
   var post = "";
   //经过req的data事件监听函数,每当接收到请求体的数据,就累加到post变量中
    req.on("data", function(chunk){
        post += chunk;
    });
    //在end事件触发后,经过querystring.parse将post解析为真正的POST请求格式,而后向客户端返回
    req.on("end", function(){
        post = querystring.parse(post);
        res.end(util.inspect(post));
    });
}).listen(3000);*/
/*

var http = require("http");
var querystring = require("querystring");

var postHTML = '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title> </head>'
    + '<body>' +
    '<form method="post">' +
    '网站名:<input name="name"><br> ' +
    '网站 URL: <input name="url">' +
    '<input type="submit">' +
    '</form> ' +
    '</body>'
'</body></html>';

http.createServer(function (req, res) {
    var body = "";
    req.on("data", function(chunk){
        body += chunk;
    });
    req.on("end", function () {
        //解析参数
        body = querystring.parse(body);
        //设置响应头部信息及编码
        res.writeHead(200, {"Content-Type":"text/html; charset=utf8"});
        //输出提交的数据
        if(body.name && body.url){
            res.write("网站名:" + body.name);
            res.write("<br>");
            res.write("网站 URL: " + body.url);
        }else{
            //输出表单
            res.write(postHTML);
        }
        res.end();
    })
}).listen(3000);

*/


var http = require("http");
var fs = require("fs");
var url = require("url");

//建立服务器
http.createServer(function(request, response){
    //解析请求,包括文件名
    var pathname = url.parse(request.url).pathname;
    //输出请求的文件名
    console.log("Request for " + pathname + " received.");
    //从文件系统中读取请求的文件内容
    fs.readFile(pathname.substr(1), function (err, data) {
        if(err){
            console.log(err);
            //HTTP 状态码:404:NOT FOUND
            //Content Type: text/plain
            response.writeHead(404, {"Content-Type": "text/html"});
        }else{
            //HTTP 状态码: 300 : OK
            //Content Type: text/plain
            response.writeHead(200, {"Content-Type":"text/html"});
            //响应文件内容
            response.write(data.toString());
        }
        //发送响应数据
        response.end();

    });
}).listen(8081);

//控制台会输出如下信息
console.log("Server running at http://localhost:8081");

client.js服务器

var http = require("http");
//用于请求的选项
var options={
    host : "localhost",
    port : "8081",
    path : "/index.html"
};

// 处理响应的回调函数
var callback = function(response){
    //不断更新数据
    var body  = "";
    response.on("data", function(data){
        body += data;
    });
    response.on("end", function () {
        //数据接收完成
        console.log(body);
    });
}

//向服务端发送请求
var req = http.request(options, callback);
req.end();
相关文章
相关标签/搜索