MangoDB创建项目-> 创建数据库
VS code, 创建Server和client文件夹,cd server -> npm init -y(创建json file 在server文件夹内)
npm install express mongoose cors nodemon
(express是framework we’re going to use to build our api, mongoose沟通MongoDB和express、node.js的桥梁,cors沟通react and backend)
Server文件夹下创建index.js,这个文件用来存储api
const express = require('express')
用来import express library
const app = express()
设定app 变量用来使用express library
测试是否布置成功, server会一直running,用ctrl+c终止
Server/package.json 添加一行start, 每次运行npm的时候会一起start nodemon,当文件变化时候,每次运行npm会检查更新,使得开发环境更加简单。
连接MongoDB,链接为mongoDB官网 -> MERN Tutorial Database Deployments -> connect -> Connect to your application( drivers) -> 最下面链接
连接MongoDB Compass, 选择database,手动创建一个database, 创建collection, 创建一条数据(add data -> insert document)
Server文件下创建models文件夹,在创建Users.js文件
Req -> request (get inf from frontend), res -> response (send inf from backend to frontend)
读写数据库时,server一定要运行状态,没有运行就在server下 node index.js

创建前端, new powershell -> cd client -> npx create-react-app . -> npm install axios -> npm start
npm is a package manager for Node.js that is used for installing and managing packages and dependencies in a project, while npx is a command-line tool that is used for executing one-off commands or scripts without requiring a separate installation.
删除一些前端不需要的文件:
创建react 命令,读写数据
前端读写,测试database:













Top comments (0)