【问题标题】:UnhandledPromiseRejectionWarning Error: Slash in host identifierUnhandledPromiseRejectionWarning 错误:主机标识符中的斜杠
【发布时间】:2018-09-15 20:23:10
【问题描述】:

我正在尝试在我的终端中运行 nodemon index.js,但我收到以下错误,我完全不知道这对我来说意味着什么非常不清楚。

谁能告诉我如何解决这个问题?

index.js

const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

var app = express();

var router = require('./services/router');

mongoose.connect('mongodb://localhost:apiAuth');

app.use(morgan('combined'));
app.use(bodyParser.json());
app.use('/v1', router);

var PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';

console.log('Listening on', HOST, PORT);
app.listen(PORT, HOST);

services/router.js

var router = require('express').Router();

function protected(req, res, next) {
    res.send('Here is the secret!');
}

router.route('/protected')
    .get(protected);

module.exports = router;

终端

[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Listening on 127.0.0.1 3000
(node:29104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Slash in host identifier
(node:29104) [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.

【问题讨论】:

  • 尝试将app.use('/v1', router); 更改为app.use('v1', router);
  • @wayneOS 还是一样的错误
  • 如指出从 v1 中删除 / 和从 /protected 中删除看起来不正确,可能尝试从中删除 /
  • @Jaya 没有任何变化
  • 我试图删除路由器,直到保持不变

标签: javascript mongodb nodemon


【解决方案1】:

我也有与上面相同的错误(错误:主机标识符中的斜杠)。我解决了这个问题。我正在访问猫鼬,如下所示。我的数据库密码包含 @,所以当我们的密码具有 @ 特殊字符时,我们需要借助 encodeURIComponent 来传递问题。我传递了用户名和密码,如下所示。

错误:

   mongoose.connect('mongodb://xxx-xx:7a?VNXd@@sabfpV8=gRLwnNvC_8@196.89.12.168:27017/xxxxx',function(err,db){
        if(err){
         console.log(err);
       }else {
           console.log('connected to the Test db');
       }
     }); 

解析代码:

 mongoose.connect('mongodb://xxx-xx:'+encodeURIComponent("7a?VNXd@#@safpV8=gRLwnNvC_8")+'@196.89.12.168:27017/xxxxx',function(err,db){
        if(err){
         console.log(err);
       }else {
           console.log('connected to the Test db');
       }
     }); 

【讨论】:

  • 谢谢!!!一个提示,如果我想要一个安全的密码,我可以使用 strongpasswordgenerator.com 并标记“避免在编程中使用标点符号”
【解决方案2】:

我遇到了类似的错误,我所做的是

以前的错误导致版本:

mongoose.connect("mongodb://localhost:cat_app");

工作版本

mongoose.connect("mongodb://localhost/cat_app");

除了 : 被 / 替换之外,两者都是相同的

【讨论】:

    【解决方案3】:

    问题来自您通过 Mongoose 连接到 MongoDB。


    短版:

    您输入了错误的登录 URL:

    mongoose.connect('mongodb://localhost:apiAuth');
                                          ^^^^^^^
    

    我想你想写(或类似的东西):

    mongoose.connect('mongodb://localhost:'+apiAuth);
    

    这是一个 MongoDB 登录 URL 的示例:mongodb://127.0.0.1:27017/my_db。 或文档:Standard Connection String Format


    加长版:

    问题的解决方法与上述相同,但您应该自己找到它。 就我而言,我是这样做的(因为我遇到了完全相同的问题,但信息量也一样多)。

    1. 隔离产生错误的代码:您只需对部分代码进行注释即可确定哪个区域崩溃了。
    2. 在与Mongoose的连接后添加catch()mongoose.connect(...).catch((e) => { console.log(e); throw e; }。这将直接表明相关线路和一些附加信息。

    这种技术在很多情况下都有效。

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多