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… Hướng dẫn này sẽ hướng dẫn bạn các bước xây dựng Node. js Restful CRUD API sử dụng Express và tương tác với cơ sở dữ liệu MySQL
Trước khi đọc hướng dẫn, vui lòng cài đặt MySQL trong máy của bạn. Hướng dẫn cài đặt có thể được tìm thấy tại Hướng dẫn cài đặt MySQL chính thức
Bài viết liên quan.
– Nút. js Ví dụ về API Rest với Express & MySQL [bao gồm Sequelize]
– Nút. js. Tải lên/Nhập dữ liệu tệp Excel vào Cơ sở dữ liệu MySQL
– Nút. js. Tải dữ liệu tệp CSV lên Cơ sở dữ liệu MySQL
Fullstack.
– Vue. js + Nút. ví dụ về js + Express + MySQL
– Vue. js + Nút. js + Express + Ví dụ về MongoDB
– Angular 8 + Nút. js Express + Ví dụ về MySQL
– Angular 10 + Nút. js Express + Ví dụ về MySQL
– Angular 11 + Nút. js Express + Ví dụ về MySQL
– Angular 12 + Nút. js Express + Ví dụ về MySQL
– Angular 13 + Nút. ví dụ về js Express + MySQL
– React + Node. ví dụ về js + Express + MySQL
Bảo mật. Nút. js – Ví dụ về xác thực và ủy quyền JWT
Triển khai.
– Triển khai/Lưu trữ nút. js trên Heroku với cơ sở dữ liệu MySQL
– Dockerize Node. js Express và ví dụ về MySQL – Docker Compose
Nút. liên kết js & MySQL.
– Ví dụ về mối quan hệ một-nhiều
– Ví dụ về mối quan hệ nhiều-nhiều
nội dung
Tổng quan về ứng dụng
Chúng tôi sẽ xây dựng Rest Apis để tạo, truy xuất, cập nhật, xóa và tìm kiếm Hướng dẫn
Đầ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 MySQL, tạo mô hình
npm install express mysql cors --save
3, 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 CRUDBả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 mysql cors --save
4POSTapi/tutorials thêm mới Hướng dẫn OUTapi/tutorials/. idupdate Hướng dẫn của npm install express mysql cors --save
4DELETEapi/tutorials/. idremove Hướng dẫn của npm install express mysql cors --save
4DELETEapi/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 mysql cors --save
7Cuối cùng, chúng ta sẽ kiểm tra Rest Apis bằng Postman
nút này. js Rest API hoạt động với Máy khách tại một trong các bài đăng này
- Máy khách HTTP đơn giản sử dụng Axios
- Máy khách HTTP đơn giản sử dụng API tìm nạp
- Góc 8 / Góc 10 / Góc 11 / Góc 12 / Góc 13
- Vue 2 / Vue 3
- Phản ứng / Phản ứng Redux
Cấu trúc dự án của chúng tôi sẽ như thế nào
Tạo nút. ứng dụng js
Mở terminal/console, sau đó tạo một thư mục cho ứng dụng của chúng ta
$ mkdir nodejs-express-mysql
$ cd nodejs-express-mysql
Khởi tạo nút. ứng dụng js với một gói. tập tin json
npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
Tiếp theo, chúng ta cần cài đặt các module cần thiết.
npm install express mysql cors --save
8, npm install express mysql cors --save
9 và {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
0. Chạy lệnh.
npm install express mysql cors --save
Gói. tệp json sẽ trông như thế này
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
Cài đặt máy chủ web Express
Bây giờ, trong thư mục gốc, chúng tôi tạo một tệp mới có tên máy chủ. js
npm install express mysql cors --save
2– nhập các mô-đun
npm install express mysql cors --save
8 và {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
0- 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 một ứng dụng Express, sau đó thêm phần mềm trung gian body-parser [
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
3 và {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
4] và {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
0 bằng phương pháp {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
6. Lưu ý rằng chúng tôi đặt nguồn gốc. {
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
7. – 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": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
8. 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.
Tạo bảng MySQL
Trước khi kết nối Node. js với MySQL, trước tiên chúng ta cần một bảng.
Vì vậy, hãy chạy tập lệnh SQL bên dưới để tạo bảng
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
9. npm install express mysql cors --save
2Định cấu hình và kết nối với cơ sở dữ liệu MySQL
Chúng ta sẽ có một thư mục riêng để cấu hình. Hãy tạo thư mục cấu hình trong thư mục ứng dụng, trong thư mục gốc của ứng dụng, sau đó tạo db. cấu hình. js bên trong thư mục cấu hình có nội dung như thế này
npm install express mysql cors --save
3Bây giờ hãy tạo kết nối cơ sở dữ liệu sử dụng cấu hình ở trên.
Tệp kết nối là db. js, chúng tôi đặt nó trong thư mục ứng dụng/mô hình sẽ chứa mô hình trong bước tiếp theo.
npm install express mysql cors --save
4Xác định mô hình
Trong thư mục models tạo 1 file có tên là tutorial. kiểu mẫu. js. Chúng ta sẽ định nghĩa hàm tạo cho đối tượng
npm install express mysql cors --save
3 ở đây và sử dụng kết nối cơ sở dữ liệu ở trên để viết các hàm CRUD- tạo một Hướng dẫn mới
- tìm Hướng dẫn theo id
- nhận tất cả các hướng dẫn
- nhận tất cả các Hướng dẫn đã xuất bản
- cập nhật Hướng dẫn theo id
- xóa Hướng dẫn
- xóa tất cả Hướng dẫn
Đây là nội dung bên trong hướng dẫn. kiểu mẫu. js
npm install express mysql cors --save
6Mô hình
npm install express mysql cors --save
3 rất đơn giản, nó chứa các trường. npm install express mysql cors --save
22, npm install express mysql cors --save
23 & npm install express mysql cors --save
24Chúng tôi sử dụng phương thức kết nối cơ sở dữ liệu
npm install express mysql cors --save
25 để thực thi tập lệnh MySQL. CHÈN, CHỌN, CẬP NHẬT, XÓA. Bạn có thể tìm thêm chi tiết về mô-đun npm install express mysql cors --save
9 tại. https. //www. npmjs. com/gói/mysqlXác định tuyến đường
Khi máy khách gửi yêu cầu cho điểm cuối bằng yêu cầu HTTP [GET, POST, PUT, DELETE], chúng ta cần xác định cách máy chủ sẽ phản hồi. Đó là lý do tại sao chúng tôi sẽ thiết lập các tuyến đường
Đây là những tuyến đường chúng tôi xác định
27. NHẬN, ĐĂNG, XÓAnpm install express mysql cors --save
28. NHẬN, ĐẶT, XÓAnpm install express mysql cors --save
Tạo thư mục định tuyến bên trong thư mục ứng dụng, sau đó tạo hướng dẫn. tuyến đường. js có nội dung như thế này
npm install express mysql cors --save
5Bạn có thể thấy rằng chúng tôi sử dụng bộ điều khiển từ
npm install express mysql cors --save
29. Nó chứa các phương thức để xử lý các hoạt động CRUD và sẽ được tạo trong bước tiếp theoChú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 mysql cors --save
20]npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
0Tạo bộ điều khiển
Bây giờ chúng tôi tạo một thư mục bộ điều khiển bên trong thư mục ứng dụng, sau đó chúng tôi có một tệp có tên hướng dẫn. bộ điều khiển. js. Bộ điều khiển của chúng tôi sẽ được viết bên trong này với các chức năng CRUD
- tạo ra
- tìm tất cả
- tìm một
- findAllPublished
- cập nhật
- xóa bỏ
- xóa hết
npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
1Hã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-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
2Truy xuất đối tượng
Truy xuất tất cả các Hướng dẫn từ cơ sở dữ liệu [có hoặc không có điều kiện]
npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
3Truy xuất một đối tượng
Tìm một Hướng dẫn duy nhất của
npm install express mysql cors --save
4npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
4Cậ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 mysql cors --save
4 trong yêu cầunpm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
5Xóa một đối tượng
Xóa Hướng dẫn có
npm install express mysql cors --save
4 được chỉ định trong yêu cầunpm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
6Xó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-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
7Kiểm tra các API
Chạy nút của chúng tôi. ứng dụng js bằng lệnh.
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
8. Bảng điều khiển hiển thị.
npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
8Sử 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
25 Apinpm install express mysql cors --save
- Truy xuất tất cả các Hướng dẫn bằng cách sử dụng
26 Apinpm install express mysql cors --save
- Truy xuất một Hướng dẫn duy nhất theo id bằng cách sử dụng
27 Apinpm install express mysql cors --save
- Cập nhật Hướng dẫn sử dụng
28 Apinpm install express mysql cors --save
- Tìm Hướng dẫn theo tiêu đề bằng cách sử dụng
26 Apinpm install express mysql cors --save
- Tìm tất cả các Hướng dẫn đã xuất bản
- Xóa Hướng dẫn bằng cách sử dụng
31 Apinpm install express mysql cors --save
- Xóa tất cả Hướng dẫn sử dụng
33 Apinpm install express mysql cors --save
Sau khi tạo một số Hướng dẫn mới, chúng ta có thể kiểm tra bảng MySQL
npm init
name: [nodejs-express-mysql]
version: [1.0.0]
description: Node.js Restful CRUD API with Node.js, Express and MySQL
entry point: [index.js] server.js
test command:
git repository:
keywords: nodejs, express, mysql, restapi
author: bezkoder
license: [ISC]
Is this ok? [yes] yes
9Kiểm tra bảng
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
9 sau khi một số hàng được cập nhậtnpm install express mysql cors --save
0Hướng dẫn với id=4 đã bị xóa khỏi bảng
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
9npm install express mysql cors --save
1Bây giờ không có hàng nào trong bảng
{
"name": "nodejs-express-mysql",
"version": "1.0.0",
"description": "Node.js Restful CRUD API with Node.js, Express and MySQL",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"express",
"mysql",
"restapi"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"mysql": "^2.18.1"
}
}
9npm install express mysql cors --save
2Bạn cũng có thể kiểm tra nút này. js Rest API với Client tại một trong những bài đăng này
- Máy khách HTTP đơn giản sử dụng Axios
- Máy khách HTTP đơn giản sử dụng API tìm nạp
- Ví dụ về ứng dụng CRUD góc 8 với API Web
- Ví dụ ứng dụng CRUD góc 10 với API Web
- Ví dụ ứng dụng CRUD góc 11 với API Web
- Ví dụ ứng dụng CRUD góc 12 với API Web
- Ví dụ ứng dụng CRUD góc 13 với API Web
- Ứng dụng Vue 2 CRUD với Vue Router & Axios
- Ứng dụng Vue 3 CRUD với Axios và Vue Router
- Phản ứng ví dụ CRUD để sử dụng API Web
- Phản ứng ví dụ Redux CRUD với lệnh gọi API
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 MySQL, tạo mô hình, 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 và phương thức tìm kiếm