Express建立基本MongoDB数据库与实现前后端连接

前言

        该栗子是在开启MongoDB服务、连接Robo 3T的前提进行的,写这篇博客是为了记录基本的nodejs框架Express搭建数据库和跑通前后端数据而写的笔记,也能分享给你。

一、下载Express、mongoose

        在新建文件夹中提前用npm下载express、mongoose

         (mongoose是nodejs中的专门用于操作mongodb数据库的js库)

npm install express --save

npm install mongoose --save

二、新建db.js文件

        用途:数据库相关的操作都在此文件

// 导入mongoose库
const mongoose = require('mongoose');

// 连接数据库
mongoose.connect('mongodb://localhost:27017/loseMg')
.then(() => {
  console.log("数据库连接成功~")
})
.catch((err) => {
  console.log("数据库连接失败~", err)
})

// 声明表结构
const LoseSchema = new mongoose.Schema({
  type: {
    type: Number
  },
  classify1: {
    type: String
  },
  classify2: {
    type: String
  },
  name: {
    type: String
  },
  date: {
    type: String
  },
  region: {
    type: String
  },
  phone: 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值