请求发射器在Express中不发出'end'

时间:2013-04-06 16:11:58

标签: node.js express

我遇到请求发射器确实发出'data'事件而不是'end'事件的情况。我不知道如何排除故障。你好吗?

var Review = require('../lib/review')
    , qs = require('querystring');

exports.processReview = function(req, res){
        var body = '';
        req.setEncoding('utf8');
        req.on('data', function(chunk){body += chunk});
        req.on('end', function(){
            console.log('got here: ' + body);
            var obj = qs.parse(body);
            var review = new Review({
                title: obj.param('title'),
                content: obj.param('content'),
                submittedBy: obj.param('submittedBy'),
                recommendedFor: obj.param('recommendedFor')
            });
            console.log('New Review: ' + JSON.stringify(review));
            res.end('Ok\n');
        });
        res.end('Ok\n');
        console.log('_________end______' + body);
    };

    exports.displayForm = function(req, res){
        var html = '<html><head><title>some head</title></head><body><h1>Review Form</h1>'
            + '<form method="post" action="/process/review" >'
            + '<p>Title: <input type="text" name="title" /></p>'
            + '<p>Content: <input type="text" name="content" /></p>'
            + '<p>Nickname: <input type="text" name="submittedBy" /></p>'
            + '<p>Recommended for: <input type="text" name="recommendedFor" /></p>'
            + '<p><input type="submit" value="Submit Review" /></p>'
            + '</form></body></html>';
        res.setHeader('Content-Type', 'text/html');
        res.setHeader('Content-Length', Buffer.byteLength(html));
        res.end(html);
    };

你会怎么去解决这个问题?有什么问题?谢谢

2 个答案:

答案 0 :(得分:3)

如果您使用express,bodyParser或任何类似的中间件,那么end在到达您的处理程序之前就已经发出了。

但就像我在评论中所说的那样,如果你使用快递,那么你也会对自己太过努力。在这种情况下,您不应该需要担心end。让中间件负责解析正文。

And by using res.send,您也可以表达其他常见任务。 send基本上是node's end的包装器。举几个例子,它是:

  • 接受HTTP状态,正文或两者作为参数
  • 将对象转换为json响应
  • 设置内容长度和其他标题
  • 计算并设置etag
  • 为遇到新缓存的请求发送304
  • 发送无头发送的头部回复

你的处理程序可以写成:

var Review = require('../lib/review');

exports.processReview = function(req, res){
  // the bodyParser (if it is in fact already a middleware) will have parsed
  // the request body already into `req.body`
  var review = new Review({
    title: req.body.title,
    content: req.body.content,
    submittedBy: req.body.submittedBy,
    recommendedFor: req.body.recommendedFor
  });
  console.log('New Review: ' + JSON.stringify(review));
  // here you'd probably save your view before sending.
  res.send(200);
};

exports.displayForm = function(req, res){
  // here you could keep your html in a template (e.g. /views/form.html), then:
  // res.render("form");

  // but even without doing that, express handles setting the content-type
  // and body length, like so:
  var html = '<html><head><title>some head</title></head><body><h1>Review Form</h1>'
    + '<form method="post" action="/process/review" >'
    + '<p>Title: <input type="text" name="title" /></p>'
    + '<p>Content: <input type="text" name="content" /></p>'
    + '<p>Nickname: <input type="text" name="submittedBy" /></p>'
    + '<p>Recommended for: <input type="text" name="recommendedFor" /></p>'
    + '<p><input type="submit" value="Submit Review" /></p>'
    + '</form></body></html>';
  res.send(html);
};

答案 1 :(得分:1)

processReview函数中,您将结束每个请求的响应。

    res.end('Ok\n'); // end response
    console.log('_________end______' + body);

请求已在res.end上完成处理,不再听取/处理任何事件。由于您无条件执行res.end,因此请求无法到达end的事件处理程序。