1.下载安装本地mongodb
2.安装mongoose
npm install mongoose
3.连接本地mongodb
const express = require("express") const mongoose = require("mongoose") const app = express() const url = require("./config/keys").mongoURI//这里是mongodb地址 mongoose.connect(url,{ useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log("连接成功")) .catch(err => console.log(err)) app.get("/",(req,res) => { res.send("Hello world!"); }) const port = process.env.PORT || 5000 //端口 app.listen(port,() => { console.log('Server runing on port $(port)') })
来源:https://www.cnblogs.com/sansuixs/p/12549937.html