简体   繁体   English

我在NodeJs服务器中无法收到POST申请

[英]I can't receive POST requisitions in my NodeJs server

I'm really really new with NodeJs (I have some knowledge of C# Asp.Net MVC and Rails, so I already got the MVC concepts). 我对NodeJ真的很陌生(我对C#Asp.Net MVC和Rails有所了解,所以我已经有了MVC概念)。 I'm following the book The Node Beginner (by Manuel Kiessling). 我正在关注《节点初学者》一书(Manuel Kiessling着)。 I just stucked at point where I created a simple form with submit button. 我只是停留在用提交按钮创建一个简单表单的地方。 The point is that I'm not catching the post in my server side. 关键是我没有在服务器端找到该职位。

This is my server code: 这是我的服务器代码:

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

function start(route, handle) {
    function onRequest(request, response) {
        var postData = "";

        var pathname = url.parse(request.url).pathname;
        console.log("Requisition received for " + pathname + ".");

        request.setEncoding("utf8");

        request.on("data", function(postDataChunk) {
            postData += postDataChunk;
            console.log("Receiving part of data from POST '" + postDataChunk + "'.");
        });

        request.addListener("end", function() {
            route(handle, pathname, response, postData);
        });
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server started");
}

exports.start = start;

When I start my application and go to localhost:8888/start I get in console all the log output. 当我启动应用程序并转到localhost:8888 / start时,我进入控制台的所有日志输出。 But when I fill the form and try submit, nothing happens. 但是,当我填写表格并尝试提交时,什么也没有发生。 控制台输出

What am I doing wrong? 我究竟做错了什么? Post is also a request, right? 发布也是一个请求,对不对? Why the line console.log("Requisition received for " + pathname + "."); 为什么会显示console.log(“收到的“ +路径名+”。“的请求); is not being executed when I clik on submit? 我点击提交后没有执行?

PS: All code and other modules can be found on my github https://github.com/ricardovsilva/nodeJs-beginnerBook PS:所有代码和其他模块都可以在我的github https://github.com/ricardovsilva/nodeJs-beginnerBook中找到

I cloned your repo and took a look at it. 我克隆了您的存储库并进行了查看。 It looks like you're missing a closing quotation mark on the content attribute of your meta tag :) 好像您在meta标签的content属性上缺少右引号:)

I really hate putting markup in magic strings like that (for this very reason), but if it's something you must do then I recommend this little trick to make it easier to read: 我真的很讨厌将标记放入这样的魔术字符串中(出于这个原因),但是如果您必须这样做,那么我建议您使用此小技巧以使其易于阅读:

var body = [
  '<html>',
  '  <head>',
  '    <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 type="submit" value="Send text" />',
  '    </form>',
  '  </body>',
  '</html>'
].join('');

That way all your tag indentation is part of the string and won't get mangle by your code formatter if you have one. 这样,您所有的标签缩进都是字符串的一部分,并且如果有的话,代码格式化程序也不会使其混乱。

With all that said, after you've done your basic learning with your book I highly recommend digging into a framework such as Express . 综上所述,在您完成本书的基础学习之后,我强烈建议您深入研究Express等框架。

PS - The "Network" tab in Chrome's developer tools is your friend :D PS-Chrome开发人员工具中的“网络”标签是您的朋友:D

You can see from there that your form isn't making a request to the server at all :) 您可以从那里看到您的表单根本没有向服务器发出请求:)

I found the problem. 我发现了问题。 When I look to html generated by inspector, I saw that form was not being created. 当我查看由检查器生成的html时,我看到未创建该表单。 Looking more deep into html in that I was generating, I saw that was faulting one ", and this was breaking the html. In real, POST was never happening. Solved this, everything is working now. I also updated the code on github. 在生成的html中更深入地看,我发现这是一个错误,而这破坏了html。实际上,POST从未发生过。解决了这一点,一切现在都在工作。我还更新了github上的代码。

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

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