首页 > 解决方案 > 将我的 Localhost API 转移到 heroku 服务器

问题描述

我尝试使用 nodejs 在我的 heroku 应用程序上发出 HTTP 请求。我的应用程序上线并且所有但不包含 HTTP 请求,然后当我单击提交表单按钮时向我发送错误消息。但是,在开发环境中一切正常。

到目前为止我已经尝试过:

如果我理解良好,一个潜在的解决方案是设置一个 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);
});

谢谢

标签: node.jsreactjsapideploymentserver

解决方案


推荐阅读