我尝试在我的heroku应用程序上使用nodejs在线发出HTTP请求。我的应用程序上线,但不包括HTTP请求,然后当我点击提交表单按钮时发送错误。但是在开发环境中一切正常。
到目前为止我尝试过:
我试图直接在我的代码中创建[herodku-domain] / api / [request],但我得到404 Not Found
。
在pakage.json中,我将代理设为“代理”:“https://domainame.herokuapp.com/”并且失败:404 (Not Found)
。
404 (Not Found)
404 not found
。如果我理解的话,一个潜在的解决方案是设置一个NGINX主机,然后将其直接连接到我的应用程序,并将用作反向代理。
我的个人资料定位我的server.js文件。
这是我的初始server.js文件:
// middleware framework
var express = require("express")
// creaet an express application
var app = express() ;
var port = process.env.PORT || 7500 ;
var databaseName = "database";
// Parse incoming request bodies in a middleware before your handlers,
// available under the req.body property.
var bodyParser = require("body-parser");
// Returns middleware that only parses json and only looks at requests
// where the Content-Type header matches the type option.
app.use(bodyParser.json()) ;
// Mongoose is a MongoDB object modeling tool
// designed to work in an asynchronous environment.
var mongoose = require("mongoose");
// provides you with a simple validation
// and query API to help you interact with your MongoDB database.
var db = mongoose.connection ;
// create a connection to [name] database
// mongoose.connect(`mongodb://localhost/${databaseName}`)
mongoose.connect('mongodb+srv://IDs-iqg1t.mongodb.set/(...)=true')
// notify if connection succed or failed
db.on ('error', console.error.bind(console, "connection fails"))
db.once('open', function () {
console.log(`mongoose : connection succeed on ${databaseName} database`);
});
// set the server on path of router folder
app.use('/api', require('./router/router.js'));
app.listen(, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
由于