Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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
Node.js 状态为200 ok时Axios发送网络错误_Node.js_Express_Vuejs2_Axios - Fatal编程技术网

Node.js 状态为200 ok时Axios发送网络错误

Node.js 状态为200 ok时Axios发送网络错误,node.js,express,vuejs2,axios,Node.js,Express,Vuejs2,Axios,我正在node.js express服务器上通过以下axios查询发送图片: axios.post(this.server + 'images', formData, { crossdomain: true, headers: { 'Content-Type': 'multipart/form-data' } } ).then(function (result) { console.log(resul

我正在node.js express服务器上通过以下axios查询发送图片:

axios.post(this.server + 'images',
    formData, {
        crossdomain: true,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }
).then(function (result) {
    console.log(result);
})
.catch(function (error) {
    console.log(error);
}); 
图片现在在服务器上,但是,请看看我在firefox控制台中看到的奇怪行为,它显示“网络错误”,而Axios正在接收200状态

这是my node.js后端Cors参数:

const cors = require("cors");
app.use(
  require("cors")({
    origin: function(origin, callback) {
      callback(null, origin);
    }
  })
);
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin); // update to match the domain you will make the request from
  //to allow cross domain requests to send cookie information.
  res.header("Access-Control-Allow-Credentials", true);
  // list of methods that are supported by the server
  res.header("Access-Control-Allow-Methods", "OPTIONS,GET,PUT,POST,DELETE");

  res.header(
    "Access-Control-Allow-Headers",
    "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, X-XSRF-TOKEN"
  );

  next();
});
这是一个大问题,因为我需要能够利用.then()响应,但我不能

我还应该做什么

  • 我的节点服务器在本地主机上启动:80
  • 我的Webpack-vue.js应用程序localhost:8080上启动
注: 这是我获取图片的节点功能,请注意,上传图片后,我将发送200状态:

app.post("/images", upload.single("file"), function(req, res, next) {
sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });

  res.sendStatus(200);
});
编辑将我的节点服务器端口更改为3000: 问题依然存在:

POST http://localhost:3000/images
[HTTP/1.1 200 OK 11ms]

Error: "Network Error"
    createError createError.js:16
    handleError xhr.js:81
我怎样才能得到答案

我需要获取重命名的图片,但无法访问.then()axios函数

请看那里:

我无法在axios中获取nico.jpg-545。然后(),它总是显示“网络错误”

编辑3: 像这样更改了我的后端参数,但运气不好:(这是webpack应用程序端口)

编辑4: 像这样更改了我的axios查询,但运气不好(这是node.js服务器端口):

编辑5:

尝试此代码时运气不佳,我需要访问响应数据,只得到“网络错误”

 const send = async () => {
            try {
                return await  axios.post(this.server + 'images',
                    formData, {
                      crossdomain: true,
                        headers: {
                            'Content-Type': 'multipart/form-data'
                        }
                    }
                )
            } catch (error) {
                console.error(error)
            }
            }

            const myFilename = send()
            console.log(myFilename);

我正在尝试切换到vue资源,我真的有足够的资源

编辑6:这似乎仍然是一个CORS问题:


编辑7:已解决

问题是我的整个CORS代码都在我的app.post(“/images”…函数中,位于node.js服务器上的我的app.js中。换句话说,CORS代码需要位于app.js node.js服务器文件的顶部

这是我的CORS代码,请注意,它现在位于图像web服务之前:

// ----------------------------------- CORS -------------------------------------------

const cors = require("cors");
app.use(require("cors")());

app.use(
  cors({
    origin: ["http://localhost:8081", "http://localhost:8081/images"],
    credentials: true
  })
);

// ----------------------------------- END OF CORS -------------------------------------------

app.post("/images", upload.single("file"), function(req, res, next) {
  console.log(req.file);

  sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });
  res.send({ filename: req.file.filename });
  // res.sendStatus(200);
}); 
// ----------------------------------- CORS -------------------------------------------

const cors = require("cors");
app.use(require("cors")());

app.use(
  cors({
    origin: ["http://localhost:8081", "http://localhost:8081/images"],
    credentials: true
  })
);

// ----------------------------------- END OF CORS -------------------------------------------

app.post("/images", upload.single("file"), function(req, res, next) {
  console.log(req.file);

  sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });
  res.send({ filename: req.file.filename });
  // res.sendStatus(200);
}); 
它现在工作完美无瑕,不再出现网络错误,我正在正确获取我的文件名!!

编辑7:已解决!!!

问题是,我的整个CORS代码都是在node.js服务器上我的app.js中的app.post(“/images”…函数下编写的。换句话说,CORS代码需要保留在app.js node.js服务器文件的顶部

这是我的CORS代码,请注意,它现在位于图像web服务之前:

// ----------------------------------- CORS -------------------------------------------

const cors = require("cors");
app.use(require("cors")());

app.use(
  cors({
    origin: ["http://localhost:8081", "http://localhost:8081/images"],
    credentials: true
  })
);

// ----------------------------------- END OF CORS -------------------------------------------

app.post("/images", upload.single("file"), function(req, res, next) {
  console.log(req.file);

  sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });
  res.send({ filename: req.file.filename });
  // res.sendStatus(200);
}); 
// ----------------------------------- CORS -------------------------------------------

const cors = require("cors");
app.use(require("cors")());

app.use(
  cors({
    origin: ["http://localhost:8081", "http://localhost:8081/images"],
    credentials: true
  })
);

// ----------------------------------- END OF CORS -------------------------------------------

app.post("/images", upload.single("file"), function(req, res, next) {
  console.log(req.file);

  sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });
  res.send({ filename: req.file.filename });
  // res.sendStatus(200);
}); 
它现在工作完美无瑕,不再出现网络错误,我正在正确获取我的文件名!!

在您的本地主机上尝试其他端口,3000或8000。我很确定您的本地主机不会让您连接到端口80。非常感谢!好的,我会尝试一下。如果这对您有效,我建议您将端口值设置为
process.env.port | | 3000
。一些主机提供商会设置
端口
环境变量Hello,C将我的节点服务器端口挂起到3000,它仍然不工作
req.file.path的值是多少?这个错误来自axios(谷歌搜索你的查找其他),但我认为问题是服务器端,你知道你回写文件,可能夏普锁定了它(你没有处理
err
e
,所以试着看一下)。当您不发送文件名时,为什么会返回文件名?请显示其余代码。
// ----------------------------------- CORS -------------------------------------------

const cors = require("cors");
app.use(require("cors")());

app.use(
  cors({
    origin: ["http://localhost:8081", "http://localhost:8081/images"],
    credentials: true
  })
);

// ----------------------------------- END OF CORS -------------------------------------------

app.post("/images", upload.single("file"), function(req, res, next) {
  console.log(req.file);

  sharp(req.file.path)
    .resize(200, 200)
    .toBuffer(function(err, buffer) {
      fs.writeFile(req.file.path, buffer, function(e) {});
    });
  res.send({ filename: req.file.filename });
  // res.sendStatus(200);
});