【发布时间】:2011-11-06 11:18:09
【问题描述】:
我正在使用来自 nodejs.org 的示例代码并尝试将响应发送到浏览器。
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("你好"); req.end();
但是 req.write("hello") 只是不将字符串输出到浏览器吗? 这不是正确的方法吗?谁能告诉我如何在views文件夹中输出对html的响应,以便我可以填充对静态html的响应。
【问题讨论】:
标签: javascript web-applications node.js