node.js - 将我的 Localhost API 转移到 heroku 服务器
问题描述
我尝试使用 nodejs 在我的 heroku 应用程序上发出 HTTP 请求。我的应用程序上线并且所有但不包含 HTTP 请求,然后当我单击提交表单按钮时向我发送错误消息。但是,在开发环境中一切正常。
到目前为止我已经尝试过:
- 本地主机连接在我的电脑上工作,所以 API 在生产中没问题。
我试图直接在我的代码中制作 [herodku-domain]/api/[request] 但我得到了
404 Not Found
.在 pakage.json 中,我将代理设置为“代理”:“ https://domainame.herokuapp.com/ ”,但它失败了:
404 (Not Found)
。- 我试图在我的 server.js 文件中包含我的所有路由:
404 (Not Found)
- 我试图将我所有的路由器、模型和模式文件合并到我的 server.js 文件中:
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);
});
谢谢
解决方案
推荐阅读
- python - Python调用函数作为来自另一个具有新值的函数的参数,Python
- node.js - 外部css和js没有加载到内部页面中nodejs express
- excel - 删除下划线并分别选择第一个值,然后在excel的另一个单元格中选择下两个值
- python - 我查找字谜的功能返回无
- python - 如何在while中使用for循环编写代码?
- r - 交叉验证:formula.character(object, env = baseenv()) 中的错误:公式无效
- mongodb - 如何插入具有 . 在使用 insertOne() mongodb 的密钥中?
- google-chrome-extension - Chrome 扩展程序 - 无需任何用户交互即可修改页面
- c++ - 无法使用 NVAPI 启用/禁用 Mosaic
- python - 如何在python中获得一组数组的每个组合