# 1 启动mongodb数据库
# 2连接本地存储表
# 3 采用mongoose连接node
node项目存储数据得接口
const express = require(‘express‘); const router = express.Router() const mongoose = require(‘mongoose‘) mongoose.connect(‘mongodb://localhost:27017/testdb‘, { useNewUrlParser: true }) const PositionModel = mongoose.model(‘positions‘, { positionName: String, }) const position = new PositionModel({ positionName: ‘就安大略‘ }) router.post(‘/save‘, (req, res) => { position.save().then((result) => { console.log(result) res.send(‘datas have saved mongodb‘) }); }) module.exports = router
# 4 再insomia中测试接口
# 5 在robo 3t可视化工具查看数据已经存入
原文地址:https://www.cnblogs.com/w-819/p/11324262.html
时间: 2024-10-12 06:43:58