var locationJSON, locationRequest;
locationJSON = {
latitude: 'mylat',
longitude: 'mylng'
};
locationRequest = {
host: 'localhost',
port: 1234,
path: '/',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
'content-length': locationJSON.length
}
};
var req;
req = http.request(options, function(res) {
var body;
body = '';
res.on('data', function(chunk) {
body += chunk;
});
return res.on('end', function() {
console.log(body);
callback(null, body);
});
});
req.on('error', function(err) {
callback(err);
});
req.write(data);
req.end();
另一方面,我有一个node.js服务器监听端口1234,它从来没有收到请求。有任何想法吗?为什么我不能使用Express来发布数据?
看起来'req.write'需要一个字符串,数组或缓冲区。所以也许我应该将我的JSON转换为数组? – Shamoon
通过JSON.stringify()运行它并将Content-Type设置为application/json。 –