当前位置: 首页 > news >正文

nuxt3连接mongodb操作

文章目录

      • 创建一个nuxt3应用
      • 添加nuxt后端服务
      • nuxt3路由
      • 创建mongo数据
      • 连接mongodb数据库
      • 补充
        • 添加显示(用v-for打印出数组)
      • nuxt-server-insert
      • mongodb删除数据
      • mongodb更改用户

创建一个nuxt3应用

  1. Node.js - v18.0.0 或更新版本
  2. 推荐使用 Visual Studio Code 以及 Volar 扩展
npx nuxi@latest init project_name

注意:这个会有node_moudle依赖文件所以创建时会很慢

在这里插入图片描述
找到C:\Windows\System32\drivers\etc并以管理员身份编辑保存hosts文件(用vscode可以编辑保存)

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost#编辑这段
185.199.108.133               raw.githubusercontent.com

哪个域名报错,可以添加或修改一下IP和域名

# GitHub520 Host Start
140.82.112.25                 alive.github.com
140.82.112.25                 live.github.com
185.199.108.154               github.githubassets.com
140.82.114.21                 central.github.com
185.199.108.133               desktop.githubusercontent.com
185.199.108.153               assets-cdn.github.com
185.199.108.133               camo.githubusercontent.com
185.199.108.133               github.map.fastly.net
199.232.69.194                github.global.ssl.fastly.net
140.82.112.4                  gist.github.com
185.199.108.153               github.io
140.82.114.3                  github.com
192.0.66.2                    github.blog
140.82.114.5                  api.github.com
185.199.108.133               raw.githubusercontent.com
185.199.108.133               user-images.githubusercontent.com
185.199.108.133               favicons.githubusercontent.com
185.199.108.133               avatars5.githubusercontent.com
185.199.108.133               avatars4.githubusercontent.com
185.199.108.133               avatars3.githubusercontent.com
185.199.108.133               avatars2.githubusercontent.com
185.199.108.133               avatars1.githubusercontent.com
185.199.108.133               avatars0.githubusercontent.com
185.199.108.133               avatars.githubusercontent.com
140.82.114.9                  codeload.github.com
54.231.200.129                github-cloud.s3.amazonaws.com
52.217.33.196                 github-com.s3.amazonaws.com
52.216.93.147                 github-production-release-asset-2e65be.s3.amazonaws.com
52.216.93.147                 github-production-user-asset-6210df.s3.amazonaws.com
52.217.207.33                 github-production-repository-file-5c1aeb.s3.amazonaws.com
185.199.108.153               githubstatus.com
64.71.144.211                 github.community
23.100.27.125                 github.dev
140.82.113.21                 collector.github.com
13.107.42.16                  pipelines.actions.githubusercontent.com
185.199.108.133               media.githubusercontent.com
185.199.108.133               cloud.githubusercontent.com
185.199.108.133               objects.githubusercontent.com# Update time: 2022-06-04T12:05:45+08:00
# Update url: https://raw.hellogithub.com/hosts
# Star me: https://github.com/521xueweihan/GitHub520
# GitHub520 Host End

添加nuxt后端服务

server文件夹中添加api文件夹,并在api文件夹中添加index.js/index.ts文件

在这里插入图片描述

注意:因为我使用的时yarn所以会有yarn.lock,根据自己的编译来

在这里插入图片描述

export default defineEventHandler((event:any)=>{return {status:200,message:"hello"}
})

这个说最基本的nuxt3的后端程序,想要访问可以使用fetch或者安装一个axios(集成了ajax)

nuxt3路由

<!--app.vue-->
<template><div><nuxt-link to="/">首页</nuxt-link><nuxt-page></nuxt-page></div>
</template>

在根目录下创建一个pages的文件夹,里面创建一个index.vue文件,这个index.vue文件nuxt会认为时根文件也就是/

<!--/pages/index.vue-->
<script setup lang="ts">
onMounted(()=>{fetch('/api').then(res=> {return res.json()}).then(async data=>{console.log(data)}).catch(err=>console.log(err))
})
</script><template><div>你好</div>
</template><style scoped></style>

在这里插入图片描述
这样,我们简单的nuxt前后端SSR应用就完成了

创建mongo数据

mongodb下载选择各自的系统版本(windows系统最好还是下载msi文件)注意: MongoDB从6.x版本开始不再自带mongosh。在这种情况下,你需要单独下载并安装mongosh
没有mongosh就下载mongosh(记住:需要任何目录都能访问mongosh那么就需要添加环境变量)

#我这里演示的是users,所以创建users数据库
use users;

在这里插入图片描述

#查看集合
show collections;

如果是没有的数据库那么一开始并没有集合,但是你可以按照自己的想法来使用集合,比如

#注意使用mongodb这样创建,它会默认创建一个唯一的id
db.user.insertOne({'name':"admin","account":"admin","password":"admin"})
#查看集合中的数据
db.user.find()

在这里插入图片描述

连接mongodb数据库

#npm i -D nuxt-mongodb
yarn add nuxt-mongodb

配置nuxt.config.ts:在项目的nuxt.config.ts文件中添加nuxt-mongodb模块到modules数组

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({compatibilityDate: '2024-04-03',devtools: { enabled: true },modules:['nuxt-mongodb']//添加
})

在这里插入图片描述
设置环境变量:在项目的根目录下创建.env文件,并设置MongoDB的连接字符串和数据库名称
在这里插入图片描述

MONGO_CONNECTION_STRING=你的数据库链接字符串
MONGO_DB=你的数据库名称
#MONGO_CONNECTION_STRING=mongodb://localhost:27017/users
#MONGO_DB=users

创建API文件:在server目录中的api文件夹中创建一个任意的文件(比如linkMongoDB.ts)来存放你的api后端服务,使用nuxt-mongodb提供的mongo对象来操作数据库

//以/server/api/linkMongoDB.ts为例
import { mongo } from '#nuxt-mongodb'
const db = mongo.db()
const response = await db.collection('你的集合名称').find()

在这里插入图片描述
没有安装此类mongodb的依赖

yarn add --dev @types/mongodb

但是以上的方法并不能构成服务而且asyncawait是一起出现的(js的异步),所以

//以/server/api/linkMongoDB.ts为例
import { mongo } from '#nuxt-mongodb'// const response = await db.collection('你的集合名称').find()export default defineEventHandle

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 线性代数 第五讲:线性方程组_齐次线性方程组_非齐次线性方程组_公共解同解方程组_详解
  • k8s-pod 实战八 (gRPC 探测详细分析)
  • Linux虚拟机搭建K8S环境
  • pbds库
  • Python 从入门到实战5(列表的其它操作)
  • Gazebo Harmonic gz-harmonic 和 ROS2 Jazzy 思考题 建图和导航 SLAM Navigation
  • 微信小程序知识点(一)
  • 视频压缩工具哪个好?无损压缩工具分享
  • C语言:基本数据类型 char, short int, int
  • java后端开发-Mybatis连接数据库步骤
  • k3s中使用GPU资源
  • CommonJS与ESModule标准
  • uni-app - - - - - 使用uview-plus详细步骤
  • 深度学习之 OpenCV 图像边缘检测算法解析及代码演示
  • 【mysql】mysql目录结构和源码和mysql基础练习
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 10个最佳ES6特性 ES7与ES8的特性
  • 2019.2.20 c++ 知识梳理
  • Docker下部署自己的LNMP工作环境
  • fetch 从初识到应用
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • input实现文字超出省略号功能
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • Java的Interrupt与线程中断
  • js
  • js学习笔记
  • rc-form之最单纯情况
  • springboot_database项目介绍
  • tweak 支持第三方库
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 搭建gitbook 和 访问权限认证
  • 观察者模式实现非直接耦合
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 我的zsh配置, 2019最新方案
  • 一个项目push到多个远程Git仓库
  • postgresql行列转换函数
  • 如何用纯 CSS 创作一个货车 loader
  • #define 用法
  • #include到底该写在哪
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • #stm32整理(一)flash读写
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #如何使用 Qt 5.6 在 Android 上启用 NFC
  • (Java入门)学生管理系统
  • (JS基础)String 类型
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (十六)串口UART
  • (十一)手动添加用户和文件的特殊权限
  • (四)opengl函数加载和错误处理
  • (一)RocketMQ初步认识
  • (转)菜鸟学数据库(三)——存储过程
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)