NodeJS console.log 有效但 response.write 无效 [英] NodeJS console.log works but response.write doesn't

查看:43
本文介绍了NodeJS console.log 有效但 response.write 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 NodeJS 的新手.

I'm new to NodeJS.

我正在阅读这本书来学习.

我在请求处理程序模块之一中使用以下代码.基本上,当我收到来自浏览器的请求时,我会调用此函数并在屏幕上显示的窗口中获取DIR"命令的内容.

I am using the following code in one of the request handler modules. Basically, When I get a request from the browser, I send a call to this function and get the contents of the "DIR" command in windows displayed on the screen.

index.js 文件是我作为 web 应用程序运行的文件

The index.js file is the one which I run as the webapp

这是index.js

var server = require("./server.js");
var router = require("./router.js");
var requestHandlers = require("./requestHandlers");

var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);

这是server.js服务器文件向路由器发送请求

This is server.js The server file sends the requests to the router

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

function start(route, handle)
{
function onRequest(request, response)
{
    //console.log("Request recieved!");
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " has been recieved");
    response.writeHead(200, {"Content-Type": "text/plain"});

    route(handle, pathname, response);

    response.end();
}

http.createServer(onRequest).listen(8888);

console.log("Server started!");

}

exports.start = start;

这是router.js这是使用在 index.js 中创建的 handle 的路由器(用于将请求映射到各自的处理程序).

This is router.js This is the router which uses the handle created in index.js (used to map the request to their respective handlers).

function route(handle, pathname, response)
{
console.log("About to route the request " + pathname);
if(typeof handle[pathname] == 'function')
{
    handle[pathname](response);
}
else
{
    console.log("No request handler found for " + pathname);
    response.writeHead(404,{"Content Type": "text/plain"});
    response.write("404 not found");
    response.end();
}
}

exports.route = route;

最后,这是处理请求的地方,在 requestHandlers.js在这里,看起来 console.log 语句工作正常,但 response.write 语句没有.

And finally this is where the requests are being handled, in the requestHandlers.js Over here, It looks like the console.log statement works fine, but the response.write statements don't.

var exec = require("child_process").exec;

function start(response)
{
    console.log("Request handler 'start' was called.");

    exec("dir", function(error, stdout, stderr) {
        response.writeHead(200,{"Content Type": "text/plain"});
        response.write("Hello Upload!");
        response.write(stdout);
        response.write("Hello!");
        console.log(stdout);
        response.end();
    });
}

function upload(response)
{
console.log("Request handler 'upload' was called.");
response.writeHead(200,{"Content Type": "text/plain"});
response.write("Hello Upload!");
response.end();
}

exports.start = start;
exports.upload = upload;

任何帮助将不胜感激

推荐答案

我认为您的请求中断的原因是它看起来像是 response.writeHead 被调用了两次,一次在 server#onRequest 中,一次在 requestHandlers#upload 中.

I think the reason your request is breaking is that it looks like response.writeHead is being called twice, once in server#onRequest and once in requestHandlers#upload.

Node.js 文档 - response.writeHead

一旦你对 Node 感觉更舒服一点,我可能会建议从 server.js 中提取任何响应编写/结束代码,并实现一种中间件方法,其中每个请求都通过每个 requestHandler 传递,直到它匹配或获得到最后.

Once you're feeling a little more comfortable with Node I'd probably recommend pulling any response writing/ending code out of server.js and implementing a middleware approach where each request gets passed through each requestHandler until it is matched or gets to the end.

希望有帮助——点头快乐!

hope that helps -- happy noding!

+1 Andreas 的评论.不是上传路由上的问题,因为这都是同步的,但是在 requestHandlers#start 中,一旦 exec 被调用,控制流就会继续返回到 server.js,在那里调用 response.end().

+1 Andreas' comment. Not the problem on the upload route since that is all synchronous, but in requestHandlers#start once exec is called the control flow continues back to server.js where response.end() is called.

这篇关于NodeJS console.log 有效但 response.write 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆