Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自NodeJS中POST请求的错误数据_Javascript_Node.js - Fatal编程技术网

Javascript 来自NodeJS中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

这是我在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') {
        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)
})