简体   繁体   English

请求到服务器node.js

[英]request to server node.js

I am learning about requests in node. 我正在了解节点中的请求。 I have created simple server 我已经创建了简单的服务器

var http=require("http");
var fs=require("fs");
http.createServer(function(req,res){


    switch(req.url){
        case '/redirect' :
            res.writeHead({"Location ":'/'});
            res.end();
            break;

        case '/form.html' :
            res.writeHead(200,{'Content-Type': 'text/html'});
            req.setEncoding("utf-8");
            var ws=fs.createReadStream("form.html")
            var body="";
            ws.pipe(body);      
            res.end(JSON.stringify(body));
            break;

        default:
        res.writeHead(200,{"Content-Type": 'text/plain'});
        res.end(JSON.stringify({
            url:req.url,
            method:req.method,
            headers:req.headers
        }));

    }

}).listen(4001)

and request script 并请求脚本

var request=require("request");
var inspect = require("util").inspect;

request('http://localhost:4001/form.html',function(err,res,body){
    if(err){
        throw err;
    }
    console.log(inspect({
        err:err,
        res:{
            statusCode:res.statusCode
        },
        body:JSON.parse(body)
    }))
});

If i understood it correctly (logic behind requests) = 1)the request script makes request(default get) to the server 2)The server script will notice request , and deterime what to do by request.url , here we are making request on /form.html 3) Server script will do /form/html case = put header into response, create read Stream and send the value into response 4) The response is sent into request script where it is represented by body argument in callback function 5) The script now do adction in callback function = console.log the response. 如果我正确理解(请求后的逻辑)= 1)请求脚本向服务器发出请求(默认获取)2)服务器脚本将注意到request,并确定由request.url执行的操作,这里我们在发出请求/form.html 3)服务器脚本将执行/ form / html case =将标头放入响应中,创建读取流并将值发送到响应中4)响应被发送到请求脚本中,该请求由回调函数5中的body参数表示)脚本现在在回调函数= console.log中执行响应。

form.html contains simple htmp form , but why the output of request script is form.html包含简单的htmp形式,但是为什么请求脚本的输出是

{ err: null,
  res: { statusCode: 200 },
  body:
   { url: '/form.html',
     method: 'GET',
     headers: { host: 'localhost:4001', connection: 'close' } } }

and not (in body) the text code that contains form.html? 而不是(在正文中)包含form.html的文本代码?

I'm not sure I understand exactly what you're trying to do in the form.html branch of your switch, but if you're just trying to serve the form.html file, you can do this: 我不确定我在交换机的form.html分支中确切了解您要执行的操作,但是如果您只是尝试提供form.html文件,则可以执行以下操作:

    case '/form.html' :
        res.writeHead(200,{'Content-Type': 'text/html'});
        var ws = fs.createReadStream("form.html");
        ws.pipe(res);
        break;

This will serve the contents of the local file form.html to the browser. 这会将本地文件form.html的内容提供给浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM