Hướng dẫn nodejs return json - nodejs trả về json
Xem thảo luận Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết Đọc Bàn luậnExpress is a back end web application framework for Node.js. It is one of the standard frameworks which is used by many developers. To install it, we will be using NPM (Node Package Manager).
Example: index.js
$ npm install express --save0 $ npm install express --save1 $ npm install express --save2 $ npm install express --save3 $ npm install express --save4 $ npm install express --save5 $ npm install express --save6 $ npm install express --save7 $ npm install express --save8 $ npm install express --save5 const express = require('express'); const app = express();0 const express = require('express'); const app = express();0 $ npm install express --save0 const express = require('express'); const app = express();3 $ npm install express --save2 $ npm install express --save3 $ npm install express --save4 $ npm install express --save5 $ npm install express --save6 $ npm install express --save7 node index.js0 $ npm install express --save7 node index.js2 node index.js3 node index.js4 $ npm install express --save7 node index.js6 node index.js7 $ npm install express --save5 const express = require('express'); const app = express();0 const express = require('express'); const app = express();0 Trong tuyến đường ‘/nhiều, chúng tôi đang trả về một đối tượng chứa nhiều cặp giá trị khóa. $ npm install express --save5 var http = require('http');7 var http = require('http');8 node index.js0 var http = require('http');8 node index.js2 node index.js3 node index.js4 var http = require('http');8 node index.js6 node index.js7 $ npm install express --save7 node index.js8 $ npm install express --save7 const express = require( 0var http = require('http');8 const express = require( 2var http = require('http');8 node index.js2 const express = require( 5node index.js4 var http = require('http');8 node index.js6 const express = require( 9$ npm install express --save7 'express' 1$ npm install express --save5 'express' 3const express = require('express'); const app = express();0
$ npm install express --save3 $ npm install express --save4 $ npm install express --save5 'express' 9); 0); const express = require('express'); const app = express();0 Trong tuyến đường ‘/mảng, chúng tôi đang trả lại một mảng các đối tượng, mỗi đối tượng có nhiều cặp giá trị khóa. Run the application using the following command from the root directory of the project. node index.js Output: Các Although the first method is sufficient for most solutions, there is another method that uses HTTP interface by Node.js and returns JSON data. Node.js comes with an in-built HTTP module, so we won’t have to install it separately as we did for express.
Example: index.jsBước 1 (làm cho HTTP sẵn sàng để sử dụng): Chúng tôi cần yêu cầu HTTP trong máy chủ của mình để có thể sử dụng nó. Điều này có thể được thực hiện đơn giản bằng cách thêm mã bên dưới vào máy chủ của chúng tôi. Bước 2 (sử dụng http và json.Stringify () để trả về dữ liệu json): Bây giờ chúng tôi sẽ sử dụng http.createserver () và json.Stringify () để trả về dữ liệu JSON từ máy chủ của chúng tôi. $ npm install express --save7 const app = express(); 2const app = express(); 3$ npm install express --save2 const app = express(); 5); $ npm install express --save7 const app = express(); 8node index.js3 $ npm install express --save00 const express = require('express'); const app = express();0 $ npm install express --save02 Trong tuyến đường ‘/mảng, chúng tôi đang trả lại một mảng các đối tượng, mỗi đối tượng có nhiều cặp giá trị khóa.Run the application using the following command from the root directory of the project. node index.js Output: Cáchttp://localhost:3000/, you will see the following output. Bước để chạy ứng dụng: Chạy ứng dụng bằng lệnh sau từ thư mục gốc của dự án. Both the methods can be used for returning JSON data from the server and both will produce the same output. |