节点js 5.5.0 - 即使在发布表单后未引发的请求对象上的数据

时间:2016-01-24 09:33:00

标签: node.js

我正在尝试下面的代码一切正常,除了"数据"事件没有被提出,因此我无法收集POST表单数据。

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

http.createServer(function(req,res){

    if (req.method === "GET")
    {
        res.writeHead(200, {"Content-Type":"text/html"});

        fs.createReadStream("./public/form.html","UTF-8").pipe(res);
    }

     if (req.method === "POST") 
     {

        var body = "";
        console.log("post received.");
        **req.on("data",function(){**
            var chunk="";
            console.log("chunk started");
            body +=chunk;
        });

        req.on("end", function(){
            console.log("end started");

            res.writeHead(200, {"Content-Type":"text/html"});

            res.end(`<!DOCTYPE html>
                <html>
                <head>
                <title>Form Results</title>
                </head>
                <body>
                <h1>Your form Results:</h1>
                <p>${body}</p>
                </body>
                </html>`);
        });

    }

}).listen(3000);

console.log("FormServer is running on port 3000");

提前致谢

0 个答案:

没有答案