Mar*_*ner 6 javascript mongodb nodemon
我试图在终端中运行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);
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
终奌站
[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.
Run Code Online (Sandbox Code Playgroud)
问题来自于您通过 Mongoose 与 MongoDB 的连接。
简洁版本 :
您输入了错误的登录 URL:
mongoose.connect('mongodb://localhost:apiAuth');
^^^^^^^
Run Code Online (Sandbox Code Playgroud)
我想你想写(或类似的东西):
mongoose.connect('mongodb://localhost:'+apiAuth);
Run Code Online (Sandbox Code Playgroud)
以下是 MongoDB 登录 URL 的示例mongodb://127.0.0.1:27017/my_db:或文档:标准连接字符串格式
长版:
问题的解决方法与上面相同,但你自己已经找到了。就我而言,我就是这样进行的(因为我有完全相同的问题,并且有同样多的信息)。
catch()与 Mongoose 连接后添加: mongoose.connect(...).catch((e) => { console.log(e); throw e; }。这将直接指示相关线路和一些附加信息。这种技术在很多情况下都有效。
| 归档时间: |
|
| 查看次数: |
4124 次 |
| 最近记录: |