[英]res.send not working for object?
這是代碼,htis是使用express框架的node.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Expressxx' });
});
router.post('/', function(req, res, next) {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
console.log(body);
});
/* res.send('Got the Post'); */
res.set('Content-Type', 'text/plain');
res.send('this is the body' + body);
res.end();
});
module.exports = router;
當我做console.log(body); 我看到了預期的數據,但在客戶端中,我所看到的只是“這就是身體”。 似乎res.send無法讀取主體obj?
謝謝....
您上面的代碼在觸發data
事件之前發送響應,因此永遠不會構建body
。 在end
事件處理程序中移動res.send
和關聯的調用應該可以為您提供所需的東西。
經過更多測試后,如果res.send是Json obj,似乎res.send將發送一個obj ...在我的情況下,它只是主體obj中的純文本。
當我在此處了解此方法時... http://expressjs.com/en/api.html#res.send
它說:body參數可以是Buffer對象,String,對象或Array。
它沒有說它必須是一個json obj ???
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.