tomato tomato - 1 month ago 7
HTTP Question

nodejs send http, then how to send callback data to index.ejs

first,nodejs code like this

var express = require('express');
var http = require('http');
var qs = require('querystring');
var router = express.Router();

/* GET home page. */
exports.index = function(req, res){
var opt = {
method: "POST",
host: "localhost",
port: 8080,
path: "/mongoresume/resumeList.json"
};

var req = http.request(opt, function (serverFeedback) {
if (serverFeedback.statusCode == 200) {
var body = "";
serverFeedback.on('data', function (data) { body += data; console.log(data);})
.on('end', function () { res.render("index",body);/*res.send(200, body); */});
}
else {
res.send(500, "error");
}
});

req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});

// req.write(data + "\n");
req.end();

};


when I use normal code like
res.send(200, body);
,it can receive data,but i want send data to index.ejs,so when I modify code like
res.render("index",body);
, it threw error

enter image description here

So what's the error means?

Answer

Your second parameter to the res.render function should be an object. You are providing a string (the body variable). Express tries to add a property to a string variable, which of course fails and causes your error message. Look up the documentation about res.render().

Comments