2.node接口搭建--连接MongoDB数据库

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)')
})

猜你喜欢

转载自www.cnblogs.com/sansuixs/p/12549937.html
今日推荐