解决这两接口废弃的代码在下方,cv即可运行
(node:11064) 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.
(node:11064) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
首先我们新建文件夹test
在当前文件夹下打开命令行窗口
依次输入以下代码
npm init
enter
npm install express mongodb --save
enter
不要以为我们的工作已经做完了
你还要开机啊
mongod --dbpath 数据库路径
enter
现在我们依赖的包已经装好了
接下来在我们新建的文件夹下面新建aap.js文件
复制以下代码运行
这里的代码是经过修改的,官网上的代码使用过程中会报两个接口即将废弃的错误
直接运行可以看到已经连接到数据库上了,并且不会报错
var express = require("express");
var app = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017/haha';
MongoClient.connect(url, {useNewUrlParser:true ,useUnifiedTopology: true},function (err,db) {
assert.equal(null,err);
console.log("Connected correctly to Server");
db.close();
})
app.get("/",function(req,res){
res.send("Hello");
})
app.listen(3000);