Javascript 来自NodeJS中POST请求的错误数据
这是我在nodejs中的简单服务器应用程序。我在处理我网站上的帖子时遇到问题 1 post=应用程序不返回任何内容Javascript 来自NodeJS中POST请求的错误数据,javascript,node.js,Javascript,Node.js,这是我在nodejs中的简单服务器应用程序。我在处理我网站上的帖子时遇到问题 1 post=应用程序不返回任何内容 2 post=来自上一篇文章的应用程序返回数据 var http = require('http'); var util = require('util'); var tenitem = []; var dataa; http.createServer(function (req, res) { if (req.method === 'GET') { r
2 post=来自上一篇文章的应用程序返回数据
var http = require('http');
var util = require('util');
var tenitem = [];
var dataa;
http.createServer(function (req, res) {
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
res.end('Hello World\n');
}
if (req.method === 'POST') {
var body = ''
console.log("POST");
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log(body)
dataa = body;
http.get('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + dataa, function (api) {
var bod = '';
api.on('data', function (chunk) {
bod += chunk;
});
api.on('end', function () {
console.log(bod)
tenitem = JSON.parse(bod);
})
})
});
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.end(tenitem.lowest_price)
}
}).listen(port, hostname, function () {
console.log('Server running');
});
tenitem
在内部函数中初始化,您试图将其发回。只需在该函数中移动res.end()
:
api.on('end', function () {
console.log(bod)
tenitem = JSON.parse(bod);
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.end(tenitem.lowest_price)
})