在我的應用程序中,我需要將動態數據發佈到我的主頁面(mani頁面意味着如果我在瀏覽器中運行我的url(localhost:3456)意味着將在該頁面中顯示一頁)。我可以如何發佈這些數據。我已經嘗試過,但是我無法發佈數據。任何人都可以幫助我解決這個問題。如何在Node js中發佈數據
app.js
var http = require('http');
var server = http.createServer(function(req, res){
res.writeHead(200, ['Content-Type', 'text/plain']);
res.write('Hello ');
res.end('World');
});
server.listen(3456);
postdata.js
var data={"errorMsg":{"errno":34,"code":"ENOENT","path":"missingFile.txt"},"date":"2013-0402T11:50:22.167Z"}
var options = {
host: 'localhost',
port: 3456,
path: '/',
method: 'POST',
data:data,
header: {
'content-type': 'application/json',
'content-length': data.length
}
};
var http=require('http');
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 is '+body);
});
});
req.on('error', function(err) {
console.log(err);
});
req.write(data);
req.end();
這將不勝感激,如果你會花時間去格式化在請求其他人尋求幫助之前,以可讀的方式(正確縮進)對您的代碼進行編碼。 –
@BretCopeland您好我已格式化我的code.Please檢查這一個 – sachin