在 cli 中启动 nodemon index.js 时收到弃用警告
deprecation warning is getting while starting nodemon index.js in cli
出现这样的错误
我正在做一个与 mongodb 连接的简单节点 js 项目。如何解决这个问题?
这是进入终端的错误
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
listening to serve 8080
Could not connect to database: { MongoNetworkError: failed to connect to server [localhost:27017] on first connect
mongo 连接(index.js)
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const config = require('./config/database');
mongoose.Promise = global.Promise;
mongoose.connect(config.uri, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
app.get('/', (req, res)=>{
res.send('<h1>hello world</h1>');
});
app.listen(8080, ()=>{
console.log('listening to serve 8080')
});
database.js 在配置文件夹中
const crypto = require('crypto').randomBytes(256).toString('hex');
module.exports= {
uri: 'mongodb://localhost:27017/' + this.db,
secret:
'crypto',
db:
'login'
}
如何解决?
mongoose.connect(config.uri, {useNewUrlParser: true}, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
应该可以,只是需要一个额外的参数
出现这样的错误
我正在做一个与 mongodb 连接的简单节点 js 项目。如何解决这个问题?
这是进入终端的错误
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
listening to serve 8080
Could not connect to database: { MongoNetworkError: failed to connect to server [localhost:27017] on first connect
mongo 连接(index.js)
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const config = require('./config/database');
mongoose.Promise = global.Promise;
mongoose.connect(config.uri, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
app.get('/', (req, res)=>{
res.send('<h1>hello world</h1>');
});
app.listen(8080, ()=>{
console.log('listening to serve 8080')
});
database.js 在配置文件夹中
const crypto = require('crypto').randomBytes(256).toString('hex');
module.exports= {
uri: 'mongodb://localhost:27017/' + this.db,
secret:
'crypto',
db:
'login'
}
如何解决?
mongoose.connect(config.uri, {useNewUrlParser: true}, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
应该可以,只是需要一个额外的参数