Express là một trong những khung web phổ biến nhất cho Node. js hỗ trợ định tuyến, phần mềm trung gian, hệ thống xem… Mongoose là một Nút dựa trên lời hứa. js ODM cho MongoDB cung cấp giải pháp đơn giản, dựa trên lược đồ để lập mô hình dữ liệu ứng dụng của chúng tôi cùng với tính năng truyền kiểu, xác thực, xây dựng truy vấn, móc logic nghiệp vụ tích hợp sẵn… Trong hướng dẫn này, tôi sẽ chỉ cho bạn từng bước xây dựng . js Restful API cho các thao tác CRUD sử dụng Express, Mongoose với cơ sở dữ liệu MongoDB Bạn nên cài đặt MongoDB trong máy của mình trước. Hướng dẫn cài đặt có thể được tìm thấy tại Hướng dẫn cài đặt MongoDB chính thức Fullstack. – MEVN. Vue. js + Nút. js + Express + Ví dụ về MongoDB – NGHĨA LÀ. Góc 8 + Nút. ví dụ về js + Express + MongoDB Angular 10 + Node. ví dụ về js + Express + MongoDB Angular 11 + Node. ví dụ về js + Express + MongoDB Angular 12 + Node. ví dụ về js + Express + MongoDB Angular 13 + Node. ví dụ về js + Express + MongoDB Angular 14 + Node. js + Express + Ví dụ về MongoDB – MERN. Phản ứng + nút. js + Express + Ví dụ về MongoDB Bảo vệ. Nút. js + MongoDB. Xác thực và ủy quyền người dùng với JWT triển khai. Docker Soạn. Nút. ví dụ về js Express và MongoDB nội dung - Nút. js MongoDB Rest CRUD API tổng quan
- Video giới thiệu
- Tạo nút. ứng dụng js
- Cài đặt máy chủ web Express
- Định cấu hình cơ sở dữ liệu MongoDB & Mongoose
- Định nghĩa Mongoose
- Xác định mô hình Mongoose
- Tạo bộ điều khiển
- Tạo một đối tượng mới
- Truy xuất đối tượng (có điều kiện)
- Truy xuất một đối tượng
- Cập nhật một đối tượng
- Xóa một đối tượng
- Xóa tất cả các đối tượng
- Tìm tất cả các đối tượng theo điều kiện
- Xác định tuyến đường
- Kiểm tra các API
- Mã nguồn
- Phần kết luận
- Đọc thêm
Nút. js MongoDB Rest CRUD API tổng quanChúng tôi sẽ xây dựng Rest Apis có thể tạo, truy xuất, cập nhật, xóa và tìm Hướng dẫn theo tiêu đề Đầu tiên, chúng tôi bắt đầu với một máy chủ web Express. Tiếp theo, chúng ta thêm cấu hình cho cơ sở dữ liệu MongoDB, tạo mô hình npm install express mongoose cors --save
4 với Mongoose, viết bộ điều khiển. Sau đó, chúng tôi xác định các tuyến để xử lý tất cả các hoạt động CRUD (bao gồm cả công cụ tìm tùy chỉnh)Bảng sau đây hiển thị thông tin tổng quan về các API còn lại sẽ được xuất MethodsUrlsActionsGETapi/tutorialsget all TutorialsGETapi/tutorials/. Hướng dẫn idget của npm install express mongoose cors --save
5POSTapi/tutorials thêm mới Hướng dẫn OUTapi/tutorials/. idupdate Hướng dẫn của npm install express mongoose cors --save
5DELETEapi/tutorials/. idremove Hướng dẫn của npm install express mongoose cors --save
5DELETEapi/tutorialsxóa tất cả Hướng dẫnGETapi/tutorials/publishedtìm tất cả Hướng dẫn đã xuất bảnGETapi/tutorials?title=[kw]tìm tất cả Hướng dẫn có tiêu đề chứa npm install express mongoose cors --save
8Cuối cùng, chúng ta sẽ kiểm tra Rest Apis bằng Postman Đây là cấu trúc dự án của chúng tôi Video giới thiệuĐây là nút của chúng tôi. js Express Hướng dẫn và demo ứng dụng MongoDB ngắn gọn, kiểm tra Rest Apis với Postman Tạo nút. ứng dụng jsĐầu tiên, chúng ta tạo một thư mục $ mkdir nodejs-express-mongodb
$ cd nodejs-express-mongodb
Tiếp theo, chúng tôi khởi tạo Node. Ứng dụng js có gói. tập tin json npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
Chúng ta cần cài đặt các mô-đun cần thiết. npm install express mongoose cors --save
9, {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
0 và {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
1. Chạy lệnh. npm install express mongoose cors --save
Gói. tệp json sẽ trông như thế này ________số 8Cài đặt máy chủ web ExpressTrong thư mục gốc, hãy tạo một máy chủ mới. tập tin js npm install express mongoose cors --save
2Những gì chúng tôi làm là. – nhập các mô-đun npm install express mongoose cors --save
9 và {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
1. - Express là để xây dựng Rest apis
- cors cung cấp phần mềm trung gian Express để kích hoạt CORS với nhiều tùy chọn khác nhau
– tạo ứng dụng Express, sau đó thêm phần mềm trung gian phân tích cú pháp nội dung ( {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
4 và {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
5) và {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
1 bằng phương pháp {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
7. Lưu ý rằng chúng tôi đặt nguồn gốc. {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
8. – xác định tuyến GET đơn giản để kiểm tra. – lắng nghe trên cổng 8080 để biết các yêu cầu đến. Bây giờ hãy chạy ứng dụng bằng lệnh. {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
9. Mở trình duyệt của bạn bằng url http. //máy chủ cục bộ. 8080/, bạn sẽ thấy. Vâng, bước đầu tiên đã xong. Chúng ta sẽ làm việc với Mongoose trong phần tiếp theo Định cấu hình cơ sở dữ liệu MongoDB & MongooseTrong thư mục app ta tạo 1 thư mục config riêng để cấu hình với db. cấu hình. tập tin js như thế này npm install express mongoose cors --save
1Định nghĩa MongooseChúng ta sẽ định nghĩa mô hình Mongoose (hướng dẫn. kiểu mẫu. js) cũng trong thư mục ứng dụng/mô hình trong bước tiếp theo Bây giờ hãy tạo ứng dụng/mô hình/chỉ mục. js với đoạn mã sau npm install express mongoose cors --save
2Đừng quên gọi phương thức npm install express mongoose cors --save
20 trong máy chủ. jsnpm install express mongoose cors --save
4Xác định mô hình MongooseTrong thư mục mô hình, tạo hướng dẫn. kiểu mẫu. tập tin js như thế này npm install express mongoose cors --save
5Mô hình Mongoose này đại diện cho bộ sưu tập hướng dẫn trong cơ sở dữ liệu MongoDB. Các trường này sẽ được tạo tự động cho từng tài liệu Hướng dẫn. _id, tiêu đề, mô tả, xuất bản, tạoAt, cập nhậtAt, __v npm install express mongoose cors --save
6Nếu bạn sử dụng ứng dụng này với giao diện người dùng cần trường id thay vì _id, bạn phải ghi đè phương thức npm install express mongoose cors --save
21 ánh xạ đối tượng mặc định thành đối tượng tùy chỉnh. Vì vậy, mô hình Mongoose có thể được sửa đổi như mã saunpm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
0Và kết quả sẽ như thế này- npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
1Xong các bước trên ta không cần viết hàm CRUD nữa, Mongoose Model hỗ trợ hết - tạo một Hướng dẫn mới. vật. tiết kiệm()
- tìm Hướng dẫn theo id. findById(id)
- truy xuất tất cả các Hướng dẫn. tìm thấy()
- cập nhật Hướng dẫn theo id. findByIdAndUpdate(id, dữ liệu)
- xóa Hướng dẫn. findByIdAndRemove(id)
- xóa tất cả Hướng dẫn. xóaMany()
- tìm tất cả các Hướng dẫn theo tiêu đề. tìm ({ tiêu đề. { $regex. RegExp mới (tiêu đề), tùy chọn $. "tôi" } })
Các chức năng này sẽ được sử dụng trong Bộ điều khiển của chúng tôi Tạo bộ điều khiểnBên trong thư mục ứng dụng/bộ điều khiển, hãy tạo hướng dẫn. bộ điều khiển. js với các hàm CRUD này - tạo ra
- tìm tất cả
- tìm một
- cập nhật
- xóa bỏ
- xóa hết
- findAllPublished
npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
2Hãy thực hiện các chức năng này Tạo một đối tượng mớiTạo và Lưu Hướng dẫn mới npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
3Truy xuất đối tượng (có điều kiện)Truy xuất tất cả các Hướng dẫn/tìm theo tiêu đề từ cơ sở dữ liệu npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
4Chúng tôi sử dụng npm install express mongoose cors --save
22 để lấy chuỗi truy vấn từ Yêu cầu và coi đó là điều kiện cho phương thức npm install express mongoose cors --save
23Truy xuất một đối tượngTìm một Hướng dẫn duy nhất với một npm install express mongoose cors --save
5npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
5Cập nhật một đối tượngCập nhật Hướng dẫn được xác định bởi npm install express mongoose cors --save
5 trong yêu cầunpm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
6Xóa một đối tượngXóa Hướng dẫn với npm install express mongoose cors --save
5 được chỉ địnhnpm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
7Xóa tất cả các đối tượngXóa tất cả các Hướng dẫn khỏi cơ sở dữ liệu npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
8Tìm tất cả các đối tượng theo điều kiệnTìm tất cả các Hướng dẫn với npm install express mongoose cors --save
27npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
9Bộ điều khiển này có thể được sửa đổi một chút để trả về phản hồi phân trang npm install express mongoose cors --save
0Bạn có thể tìm thêm thông tin chi tiết tại. Phân trang phía máy chủ trong nút. j với MongoDB & Mongoose Xác định tuyến đườngKhi khách hàng gửi yêu cầu cho điểm cuối bằng yêu cầu HTTP (GET, POST, PUT, DELETE), chúng tôi cần xác định cách máy chủ sẽ phản hồi bằng cách thiết lập các tuyến Đây là những tuyến đường của chúng tôi npm install express mongoose cors --save
28. NHẬN, ĐĂNG, XÓAnpm install express mongoose cors --save
29. NHẬN, ĐẶT, XÓAnpm install express mongoose cors --save
10. ĐƯỢC
Tạo một hướng dẫn. tuyến đường. js bên trong thư mục app/routes với nội dung như thế này npm install express mongoose cors --save
1Bạn có thể thấy rằng chúng tôi sử dụng bộ điều khiển từ npm install express mongoose cors --save
11Chúng tôi cũng cần bao gồm các tuyến trong máy chủ. js (ngay trước npm install express mongoose cors --save
12)npm install express mongoose cors --save
2Kiểm tra các APIChạy nút của chúng tôi. ứng dụng js bằng lệnh. {
"name": "node-express-mongodb",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MongoDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"rest",
"api",
"mongodb"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mongoose": "^6.4.2"
}
}
9Sử dụng Postman, chúng ta sẽ kiểm tra tất cả các Apis ở trên - Tạo Hướng dẫn mới bằng cách sử dụng
npm install express mongoose cors --save
14 Api
Sau khi tạo một số Hướng dẫn mới, bạn có thể kiểm tra bộ sưu tập MongoDb - Truy xuất một Hướng dẫn duy nhất theo id bằng cách sử dụng
npm install express mongoose cors --save
15 Api
- Cập nhật Hướng dẫn sử dụng
npm install express mongoose cors --save
16 Api
Kiểm tra bộ sưu tập npm install express mongoose cors --save
17 sau khi một số tài liệu được cập nhật- Tìm tất cả các Hướng dẫn có tiêu đề chứa 'js'.
npm install express mongoose cors --save
18
- Tìm tất cả các Hướng dẫn đã xuất bản bằng cách sử dụng
npm install express mongoose cors --save
19 Api
- Xóa Hướng dẫn sử dụng
npm install express mongoose cors --save
20 Api
Hướng dẫn đã bị xóa khỏi bộ sưu tập npm install express mongoose cors --save
17- Xóa tất cả Hướng dẫn sử dụng
npm install express mongoose cors --save
22 Api
Bạn có thể sử dụng Máy khách HTTP đơn giản bằng Axios để kiểm tra Hoặc. Máy khách HTTP đơn giản sử dụng API tìm nạp Phần kết luậnHôm nay, chúng ta đã học cách tạo Node. js Rest Apis với máy chủ web Express. Chúng tôi cũng biết cách thêm cấu hình cho cơ sở dữ liệu MongoDB & Mongoose, tạo Mô hình Mongoose, viết bộ điều khiển và xác định các tuyến để xử lý tất cả các hoạt động CRUD |