Heroku + MERN堆栈 - HTTP POST请求失效应该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (119)

尝试将HTTP POST请求发送到我的Mlab数据库。

在生产环境中,我的请求被发送失效。

使用MongoDB + Express + Reactjs + Node - MERN堆栈 - + Heroku PaaS。

我在会话开始时收到了以下消息:

Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script 

但是我现在不再收到此消息:我已经在每次重新加载时更新了服务工作人员,并将我的serviceworker.js副本移到了我的HTML文件夹中。

为什么我发送HTTP POST请求时仍然无效?

关于我的日志,似乎我的服务人员和file.map存在问题:

2018-05-24T18:25:39.795372+00:00 heroku[router]: at=info method=GET path="/static/js/main.3685cd33.js.map" host=#.com request_id=03674121-2fac-42ec-9459-e5fa17eb2ea7 fwd="93.27.27.184" dyno=web.1 connect=0ms service=7ms status=404 bytes=413 protocol=https 2018-05-24T18:25:41.576623+00:00 heroku[router]: at=info method=GET path="/service- worker.js" host=#.herokuapp.com request_id=065fb0eb-6606-4603- 8673-75e59445a1ea fwd="93.27.27.184" dyno=web.1 connect=0ms service=1ms status=404 bytes=400 protocol=https 2018-05-24T18:25:41.902150+00:00 heroku[router]: at=info method=GET path="/static/css/main.969d7734.css.map" host=#.herokuapp.com request_id=a16b6a78-9167-4672-9888-3f141a46c3ef fwd="93.27.27.18

4" dyno=web.1 connect=0ms service=1ms status=404 bytes=415 protocol=https

这里我的App.js:

  (...)
alert("word = "+ this.state.word) //  **SO FAR, THE VALUE IS STILL DISPLAYED**
fetch("/api/words",{
 method:"POST",
 headers: {
   'Accept': 'application/json, text/plain, */*',
   'Content-Type':'application/json'
 },
 body:JSON.stringify({ // **THEN I LOST THE CONTACT WITH THE VALUES FROM HERE**
                       "word":this.state.word,
                       "definition":this.state.definition,
                       "sound":this.state.audioRecorded
                     })
})
.then((res) => res.json())
.then((data) => console.log(data))

  }


   <form>
    (...)
   </form>
  )
 } 
 };

我的server.js:

const {createServer} = require("http");
var express = require("express")
var path = require("path")
var app = express() ;
var port = process.env.PORT || 7500 ;
var databaseName = "ntsat";
var bodyParser = require("body-parser");
app.use(bodyParser.json()) ;
app.use(express.static(path.join(__dirname, '../react-ui/public/index.html')));
var mongoose = require("mongoose");
var url = process.env.MONGOLAB_URI;
mongoose.connect([url to my mlab])
var db = mongoose.connection;
db.on ("error", console.error.bind(console, "connection fails"))
db.once("open", function () {
console.log(`mongoose : connection succeed on ${databaseName} database`); 
});
 app.use("/api", require("./router/router.js"));
 app.listen( port, function(){
   console.log("Express server listening on port %d in %s mode", this.address().port, 
 app.settings.env); 
 });
 module.exports = app;
提问于
用户回答回答于

扫码关注云+社区

领取腾讯云代金券