微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node.js 服务器 POST 请求只有在之前有 GET 请求时才会通过——不知道为什么?

如何解决Node.js 服务器 POST 请求只有在之前有 GET 请求时才会通过——不知道为什么?

const express = require('express');
const app = express();
const http = require('http').Server(app);
const port = process.env.PORT || 5000;

app.use(express.static(__dirname + '/public'));

http.on('request',(request,response) => {

  console.log("the method request was " + request.method) //either prints POST or GET depending on what I'm sending it
  if (request.method == "GET") {
    console.log("GET")
  } else if (request.method == "POST") {
    console.log("POST")
  }

  request.on('error',(err) => {
    // This prints the error message and stack trace to `stderr`.
    console.error("the error was " + err);
  });
  // console.log("got a request to the server")
  response.send("response string")
});

http.listen(port,() => console.log('listening on port ' + port));

所以基本上,问题是如果我发送 GET 请求,然后发送 POST 请求,它会打印所有正确的内容。但奇怪的是,如果我只是先发送 POST 请求,则没有打印输出。如果您需要查看我的客户端 Swift 代码,请告诉我,我会添加--谢谢!

可以在此处查看当前问题的视频:https://youtu.be/Rac9N9F4KDo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。