2
我正在使用来自nodejs.org的示例代码并尝试将响应发送到浏览器。 (“hello”);如何发送响应浏览器fromm http.request node.js?
var http = require("http");
var port = 8001;
http.createServer().listen(port);
var options = {
host: "xxx",
port: 5984,
//path: "/_all_dbs",
path: "xxxxx",
method: "GET"
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
var buffer = "";
buffer += chunk;
var parsedData = JSON.parse(buffer);
console.log(parsedData);
console.log("Name of the contact "+parsedData.name);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(“hello”); req.end();
但req.write(“hello”)只是不会输出字符串到浏览器? 这不正确吗?有人也可以告诉我如何输出响应到视图文件夹中的HTML,以便我可以填充对静态html的响应。
非常感谢。只是另一个问题。你知道如何在视图文件夹中的.htm中显示数据(在这种情况下是块)。 – Preethi
添加了一个将输出保存到文件的脚本 – stewe