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
Đầy đủ ngăn xếp
– MEVN. Vue. js + Nút. ví dụ về js + Express + MongoDB
- NGHĨA LÀ
Góc 8 + Nút. ví dụ về js + Express + MongoDB
Góc 10 + Nút. ví dụ về js + Express + MongoDB
Góc 11 + Nút. ví dụ về js + Express + MongoDB
Góc 12 + Nút. ví dụ về js + Express + MongoDB
Góc 13 + Nút. ví dụ về js + Express + MongoDB
Góc 14 + Nút. ví dụ về js + Express + MongoDB
– MERN. Phản ứng + nút. ví dụ về js + Express + 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
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
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"
}
}
1Chạ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
const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
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 một ứng dụng Express, sau đó thêm phần mềm trung gian phân tích cú pháp cơ thể [_______8_______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 cho 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"
}
}
9Mở trình duyệt của bạn với 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 & 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
module.exports = {
url: "mongodb://localhost:27017/bezkoder_db"
};
Định nghĩa Mongoose
Chúng ta sẽ định nghĩa mô hình Mongoose [hướng dẫn. người 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
const dbConfig = require["../config/db.config.js"];
const mongoose = require["mongoose"];
mongoose.Promise = global.Promise;
const db = {};
db.mongoose = mongoose;
db.url = dbConfig.url;
db.tutorials = require["./tutorial.model.js"][mongoose];
module.exports = db;
Đừng quên gọi phương thức
const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
0 trong máy chủ. js...
const app = express[];
app.use[...];
const db = require["./app/models"];
db.mongoose
.connect[db.url, {
useNewUrlParser: true,
useUnifiedTopology: true
}]
.then[[] => {
console.log["Connected to the database!"];
}]
.catch[err => {
console.log["Cannot connect to the database!", err];
process.exit[];
}];
Xác định mô hình Mongoose
Trong thư mục mô hình, tạo hướng dẫn. người mẫu. tập tin js như thế này
module.exports = mongoose => {
const Tutorial = mongoose.model[
"tutorial",
mongoose.Schema[
{
title: String,
description: String,
published: Boolean
},
{ timestamps: true }
]
];
return Tutorial;
};
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
{
"_id": "5e363b135036a835ac1a7da8",
"title": "Js Tut#",
"description": "Description for Tut#",
"published": true,
"createdAt": "2020-02-02T02:59:31.198Z",
"updatedAt": "2020-02-02T02:59:31.198Z",
"__v": 0
}
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
const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
1 á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. sự vật
- tìm Hướng dẫn theo id. [nhận dạng]
- truy xuất tất cả các Hướng dẫn.
- cập nhật Hướng dẫn theo id. [id, dữ liệu]
- xóa Hướng dẫn. [nhận dạng]
- xóa tất cả Hướng dẫn.
- 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 nên
- 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ớ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
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
const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
2 để lấy chuỗi truy vấn từ Yêu cầu và coi đó là điều kiện cho phương thức const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
3Truy 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
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ượ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ầ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ượng
Xóa Hướng dẫn với
npm install express mongoose cors --save
5 đã 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ượ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
8Tì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
const express = require["express"];
const cors = require["cors"];
const app = express[];
var corsOptions = {
origin: "//localhost:8081"
};
app.use[cors[corsOptions]];
// parse requests of content-type - application/json
app.use[express.json[]];
// parse requests of content-type - application/x-www-form-urlencoded
app.use[express.urlencoded[{ extended: true }]];
// simple route
app.get["/", [req, res] => {
res.json[{ message: "Welcome to bezkoder application." }];
}];
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen[PORT, [] => {
console.log[`Server is running on port ${PORT}.`];
}];
7npm 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 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
8. NHẬN, ĐĂNG, XÓAconst express = require["express"]; const cors = require["cors"]; const app = express[]; var corsOptions = { origin: "//localhost:8081" }; app.use[cors[corsOptions]]; // parse requests of content-type - application/json app.use[express.json[]]; // parse requests of content-type - application/x-www-form-urlencoded app.use[express.urlencoded[{ extended: true }]]; // simple route app.get["/", [req, res] => { res.json[{ message: "Welcome to bezkoder application." }]; }]; // set port, listen for requests const PORT = process.env.PORT || 8080; app.listen[PORT, [] => { console.log[`Server is running on port ${PORT}.`]; }];
9. NHẬN, ĐẶT, XÓAconst express = require["express"]; const cors = require["cors"]; const app = express[]; var corsOptions = { origin: "//localhost:8081" }; app.use[cors[corsOptions]]; // parse requests of content-type - application/json app.use[express.json[]]; // parse requests of content-type - application/x-www-form-urlencoded app.use[express.urlencoded[{ extended: true }]]; // simple route app.get["/", [req, res] => { res.json[{ message: "Welcome to bezkoder application." }]; }]; // set port, listen for requests const PORT = process.env.PORT || 8080; app.listen[PORT, [] => { console.log[`Server is running on port ${PORT}.`]; }];
0. LẤYmodule.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
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ừ
module.exports = {
url: "mongodb://localhost:27017/bezkoder_db"
};
1Chúng tôi cũng cần bao gồm các tuyến trong máy chủ. js [ngay trước
module.exports = {
url: "mongodb://localhost:27017/bezkoder_db"
};
2]npm install express mongoose cors --save
2Kiể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"
}
}
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
4 Apimodule.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
- Truy xuất một Hướng dẫn duy nhất theo id bằng cách sử dụng
5 Apimodule.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
- Cập nhật Hướng dẫn sử dụng
6 Apimodule.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
- Tìm tất cả các Hướng dẫn có tiêu đề chứa 'js'.
8module.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
- Tìm tất cả các Hướng dẫn đã xuất bản bằng cách sử dụng
9 Apimodule.exports = { url: "mongodb://localhost:27017/bezkoder_db" };
- Xóa Hướng dẫn bằng cách sử dụng
0 Apiconst dbConfig = require["../config/db.config.js"]; const mongoose = require["mongoose"]; mongoose.Promise = global.Promise; const db = {}; db.mongoose = mongoose; db.url = dbConfig.url; db.tutorials = require["./tutorial.model.js"][mongoose]; module.exports = db;
- Xóa tất cả Hướng dẫn sử dụng
2 Apiconst dbConfig = require["../config/db.config.js"]; const mongoose = require["mongoose"]; mongoose.Promise = global.Promise; const db = {}; db.mongoose = mongoose; db.url = dbConfig.url; db.tutorials = require["./tutorial.model.js"][mongoose]; module.exports = db;
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
Kiểm tra bộ sưu tập
module.exports = {
url: "mongodb://localhost:27017/bezkoder_db"
};
7 sau khi một số tài liệu được cập nhậtHướng dẫn đã bị xóa khỏi bộ sưu tập
module.exports = {
url: "mongodb://localhost:27017/bezkoder_db"
};
7Bạ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ậ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