API nodejs MongoDB

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 quan

Chú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
8

Cuố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

API nodejs MongoDB
API nodejs MongoDB

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ố 8

Cài đặt máy chủ web Express

Trong 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
2

Nhữ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.

API nodejs MongoDB
API nodejs MongoDB

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 & Mongoose

Trong 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 Mongoose

Chú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ủ. js

npm install express mongoose cors --save
4

Xác định mô hình Mongoose

Trong 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
5

Mô 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
6

Nế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ã sau

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
0

Và 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
1

Xong 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ển

Bê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
2

Hãy thực hiện các chức năng này

Tạo một đối tượng mới

Tạ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
3

Truy 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
4

Chú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
23

Truy xuất một đối tượng

Tìm một Hướng dẫn duy nhất với một

npm install express mongoose cors --save
5

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
5

Cập nhật một đối tượng

Cập nhật Hướng dẫn được xác định bởi

npm install express mongoose cors --save
5 trong yêu cầ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
6

Xóa một đối tượng

Xóa Hướng dẫn với

npm install express mongoose cors --save
5 được chỉ định

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
7

Xóa tất cả các đối tượng

Xó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
8

Tìm tất cả các đối tượng theo điều kiện

Tìm tất cả các Hướng dẫn với

npm install express mongoose cors --save
27

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
9

Bộ đ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
0

Bạ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 đường

Khi 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ÓA
  • npm install express mongoose cors --save
    
    29. NHẬN, ĐẶT, XÓA
  • npm 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
1

Bạ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
11

Chú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
2

Kiểm tra các API

Chạ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"
  }
}
9

Sử dụng Postman, chúng ta sẽ kiểm tra tất cả các Apis ở trên

  1. Tạo Hướng dẫn mới bằng cách sử dụng
    npm install express mongoose cors --save
    
    14 Api

  2. API nodejs MongoDB
    API nodejs MongoDB

    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

    API nodejs MongoDB
    API nodejs MongoDB

  3. 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

  4. API nodejs MongoDB
    API nodejs MongoDB

  5. Cập nhật Hướng dẫn sử dụng
    npm install express mongoose cors --save
    
    16 Api

  6. API nodejs MongoDB
    API nodejs MongoDB

    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

    API nodejs MongoDB
    API nodejs MongoDB

  7. Tìm tất cả các Hướng dẫn có tiêu đề chứa 'js'.
    npm install express mongoose cors --save
    
    18

  8. API nodejs MongoDB
    API nodejs MongoDB

  9. 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

  10. API nodejs MongoDB
    API nodejs MongoDB

  11. Xóa Hướng dẫn sử dụng
    npm install express mongoose cors --save
    
    20 Api

  12. API nodejs MongoDB
    API nodejs MongoDB

    Hướng dẫn đã bị xóa khỏi bộ sưu tập

    npm install express mongoose cors --save
    
    17

    API nodejs MongoDB
    API nodejs MongoDB

  13. Xóa tất cả Hướng dẫn sử dụng
    npm install express mongoose cors --save
    
    22 Api

  14. API nodejs MongoDB
    API nodejs MongoDB

Bạn có thể sử dụng Máy khách HTTP đơn giản bằng Axios để kiểm tra

API nodejs MongoDB
API nodejs MongoDB

Hoặc. Máy khách HTTP đơn giản sử dụng API tìm nạp

Phần kết luận

Hô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