简体   繁体   English

Node.js 项目部署在 Heroku 上。 无法访问 API 端点并与 MongoDB Atlas 连接。 返回状态 404

[英]Node.js project deployed on Heroku. Can't access API endpoints and connect with MongoDB Atlas. Returning status 404

I tried to find a question similar to mine and I'm looking at google, the logs and try to fix this issue whole day without any success.我试图找到一个类似于我的问题,我正在查看谷歌,日志并尝试整天解决这个问题,但没有任何成功。 I hope you guys can help me.我希望你们能帮助我。

I have Node.js API with Express deployed on Heroku and I'm trying to connect it with MongoDB Atlas.我有 Node.js API 和 Express 部署在 Heroku 上,我正在尝试将它与 Z206E3718AF092FD1DZ12F08 连接At the beginning at the logs was a problem with the production variables, which I sorted out.最初在日志中是生产变量的问题,我对此进行了整理。 But still, when I try to access any API endpoint this is the logs I get:但是,当我尝试访问任何 API 端点时,这是我得到的日志:

2021-01-23T17:49:34.346738+00:00 heroku[web.1]: Starting process with command `npm start`
2021-01-23T17:49:36.180974+00:00 app[web.1]:
2021-01-23T17:49:36.180991+00:00 app[web.1]: > eastern-beauty-backend@1.0.0 start /app
2021-01-23T17:49:36.180991+00:00 app[web.1]: > node index.js
2021-01-23T17:49:36.180991+00:00 app[web.1]:
2021-01-23T17:49:36.921969+00:00 app[web.1]: [winston] Attempt to write logs with no transports {"message":"Listening on port 31690...","level":"info"}
2021-01-23T17:49:36.926481+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
2021-01-23T17:49:36.926482+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:565:21)
2021-01-23T17:49:36.926483+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/connect.js:282:3)
2021-01-23T17:49:36.926484+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:224:5
2021-01-23T17:49:36.926484+00:00 app[web.1]: at maybePromise (/app/node_modules/mongodb/lib/utils.js:665:3)
2021-01-23T17:49:36.926484+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:220:10)
2021-01-23T17:49:36.926485+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:833:12
2021-01-23T17:49:36.926486+00:00 app[web.1]: at new Promise (<anonymous>)
2021-01-23T17:49:36.926486+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:830:19)
2021-01-23T17:49:36.926486+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:345:10
2021-01-23T17:49:36.926486+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
2021-01-23T17:49:36.926487+00:00 app[web.1]: at new Promise (<anonymous>)
2021-01-23T17:49:36.926487+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
2021-01-23T17:49:36.926487+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1135:10)
2021-01-23T17:49:36.926488+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:344:20)
2021-01-23T17:49:36.926488+00:00 app[web.1]: at module.exports (/app/startup/db.js:7:12)
2021-01-23T17:49:36.926488+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:9:24)
2021-01-23T17:49:36.926591+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
2021-01-23T17:49:36.926651+00:00 app[web.1]: (node:21) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2021-01-23T17:49:37.268649+00:00 heroku[web.1]: State changed from starting to up
2021-01-23T17:49:38.509600+00:00 heroku[router]: at=info method=GET path="/" host=eastern-beauty-api.herokuapp.com request_id=d4f01cf5-9a03-43c0-b90c-502e1526d2b6 fwd="148.252.132.237" dyno=web.1 connect=0ms service=10ms status=404 bytes=415 protocol=https
2021-01-23T17:49:43.261992+00:00 heroku[router]: at=info method=GET path="/users" host=eastern-beauty-api.herokuapp.com request_id=7b2758e9-d3d9-4c5d-b693-af513424e7bd fwd="148.252.132.237" dyno=web.1 connect=0ms service=2ms status=404 bytes=420 protocol=https
2021-01-23T17:50:06.222493+00:00 heroku[router]: at=info method=GET path="/users" host=eastern-beauty-api.herokuapp.com request_id=dc17bb7f-8cb9-4e2b-ac5a-054edb95fb27 fwd="148.252.132.237" dyno=web.1 connect=0ms service=2ms status=404 bytes=420 protocol=https  

and the status returned is 404 "Not found".并且返回的状态是 404“未找到”。

It works fine in development mode.它在开发模式下工作正常。 I set the custom environment variable for connection with the MongoDB Atlas, and I replace the username, password and dbname where required.我设置了与 MongoDB Atlas 连接的自定义环境变量,并在需要时替换了用户名、密码和 dbname。

mongodb+srv://<username>:<password>@eb.bvm0d.mongodb.net/<dbname>?retryWrites=true&w=majority

I tried to connect with Shell and there were no issues.我尝试连接 Shell 并且没有问题。 I'm considering something to be wrong in my code but I can't spot anything.我正在考虑我的代码有问题,但我什么也找不到。

Here are some of the files I have:以下是我拥有的一些文件:

index.js index.js

const winston = require("winston");
const express = require("express");
const config = require("config");
const app = express();


require("./startup/cors")(app);
require("./startup/routes")(app);
require("./startup/db")();
require("./startup/config")();
require("./startup/validation")();

const port = process.env.PORT || config.get("port");
const server = app.listen(port, () =>
  winston.info(`Listening on port ${port}...`)
);

module.exports = server;

startup/routes.js启动/routes.js

const express = require('express');
const error = require('../middleware/error');
const auth = require('../routes/auth');
const users = require('../routes/users');
const customers = require('../routes/customers');
const herbs = require('../routes/herbs');
const herbsBG = require('../routes/herbsBG');
const oils = require('../routes/oils');
const oilsBG = require('../routes/oilsBG');

module.exports = function(app) {
  app.use(express.json());
  app.use('/api/auth', auth);
  app.use('/api/users', users);
  app.use('/api/customers', customers);
  app.use('/api/herbs', herbs);
  app.use('/api/herbsBG', herbsBG);
  app.use('/api/oils', oils);
  app.use('/api/oilsBG', oilsBG);
  app.use(error);
}

startup/db.js启动/db.js

const winston = require('winston');
const mongoose = require('mongoose');
const config = require('config');

module.exports = function() {
  const db = config.get('db');
  mongoose.connect(db, { useNewUrlParser: true })
    .then(() => winston.info(`Connected to ${db}...`));
}

config/default.json配置/默认.json

{
  "jwtPrivateKey": "unsecureKey",
  "db": "mongodb://localhost/ebeauty",
  "port": "3900",
  "requiresAuth": false
}

config/production.json配置/生产.json

{
  "jwtPrivateKey": "EB_jwtPrivateKey",
  "db": "eb_db"
}

Inside the production.json, I declare the custom environment variables.在production.json里面,我声明了自定义环境变量。 The variables are at Heruko, and the idea is to be hidden for security purposes.变量在 Heruko,出于安全目的,这个想法是隐藏的。 Then under "eb_db" on Heroku, I place the URL for the connection with MongoDB.然后在 Heroku 上的“eb_db”下,我放置 URL 用于与 MongoDB 的连接。

Thanks for your help!谢谢你的帮助!

The relevant error there is有相关的错误

UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string UnhandledPromiseRejectionWarning: MongoParseError: 无效的连接字符串

The location was位置是

at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:565:21)在 parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:565:21)

If you are using the latest version of the driver, that code is (from github ):如果您使用的是最新版本的驱动程序,该代码是(来自github ):

      if (parsedHost.path === '/:') {
        hostParsingError = new MongoParseError('Double colon in host identifier');
        return null;
      }

That would seem to imply an unescaped special character or a missing environment variable.这似乎意味着未转义的特殊字符或缺少环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM