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

  • Đọc
  • Bàn luận
  • 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).

    • JSON là viết tắt của ký hiệu đối tượng JavaScript. Đây là một trong những định dạng được sử dụng rộng rãi nhất để trao đổi thông tin trên các ứng dụng. Node.js hỗ trợ các khung khác nhau giúp làm cho các quy trình mượt mà hơn. Các cách sau bao gồm cách trả về dữ liệu JSON trong ứng dụng của chúng tôi từ Node.js. To install npm in a project, head over to the terminal and change your current working directory to that project. Then, use the npm init command to initialize the package.json file.

      $ cd {your-project-directory}
      $ npm init
    • Phương thức 1 (sử dụng Express.js): Express là khung ứng dụng web cuối cùng cho Node.js. Đây là một trong những khung tiêu chuẩn được sử dụng bởi nhiều nhà phát triển. Để cài đặt nó, chúng tôi sẽ sử dụng NPM (Trình quản lý gói nút). Now that we have npm installed in our project, we can install Express by using the npm install command in our terminal.

      $ npm install express --save
    • Bước 1 (Cài đặt NPM trong thư mục của chúng tôi): Để cài đặt NPM trong một dự án, hãy đến thiết bị đầu cuối và thay đổi thư mục làm việc hiện tại của bạn thành dự án đó. Sau đó, sử dụng lệnh npm init để khởi tạo tệp pack.json. After installing express, we can start writing code on our server. We need to require an express module before using it, and we can do that by adding the code given below at the top of our server code.

      const express = require('express');
      const app = express();
    • Bước 2 (Cài đặt Express): Bây giờ chúng tôi đã cài đặt NPM trong dự án của mình, chúng tôi có thể cài đặt Express bằng cách sử dụng lệnh Cài đặt NPM trong thiết bị đầu cuối của chúng tôi. Now our express is completely ready to use. In the example given below, we are returning data in the following manner:

      • Bước 3 (Sẵn sàng sử dụng Express để sử dụng): Sau khi cài đặt Express, chúng tôi có thể bắt đầu viết mã trên máy chủ của mình. Chúng ta cần yêu cầu một mô -đun rõ ràng trước khi sử dụng và chúng ta có thể làm điều đó bằng cách thêm mã được đưa ra bên dưới ở đầu mã máy chủ của chúng ta.
      • Bước 4 (sử dụng Express để trả về dữ liệu JSON): Bây giờ chúng tôi Express hoàn toàn sẵn sàng sử dụng. Trong ví dụ được đưa ra dưới đây, chúng tôi đang trả về dữ liệu theo cách sau:
      • Trong tuyến đường ‘/, chúng tôi đang trả về một đối tượng chứa một cặp giá trị khóa duy nhất.

    Example:

    index.js

    const express = require('express');

    const app = express();

    $ npm install express --save
    0
    $ npm install express --save
    1
    $ npm install express --save
    2
    $ npm install express --save
    3
    $ npm install express --save
    4

    $ npm install express --save
    5
    $ npm install express --save
    6

    $ npm install express --save
    7
    $ npm install express --save
    8

    $ npm install express --save
    5
    const express = require('express');
    const app = express();
    0

    const express = require('express');
    const app = express();
    0

    $ npm install express --save
    0
    const express = require('express');
    const app = express();
    3
    $ npm install express --save
    2
    $ npm install express --save
    3
    $ npm install express --save
    4

    $ npm install express --save
    5
    $ npm install express --save
    6

    $ npm install express --save
    7
    node index.js
    0

    $ npm install express --save
    7
    node index.js
    2
    node index.js
    3
    node index.js
    4

    $ npm install express --save
    7
    node index.js
    6
    node index.js
    7

    $ npm install express --save
    5
    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 --save
    5
    var http = require('http');
    7

    var http = require('http');
    8
    node index.js
    0

    var http = require('http');
    8
    node index.js
    2
    node index.js
    3
    node index.js
    4

    var http = require('http');
    8
    node index.js
    6
    node index.js
    7

    $ npm install express --save
    7
    node index.js
    8

    $ npm install express --save
    7const express = require(0

    var http = require('http');
    8const express = require(2

    var http = require('http');
    8
    node index.js
    2const express = require(5
    node index.js
    4

    var http = require('http');
    8
    node index.js
    6const express = require(9

    $ npm install express --save
    7'express'1

    $ npm install express --save
    5'express'3

    const express = require('express');
    const app = express();
    0

    'express'5

    $ npm install express --save
    3
    $ npm install express --save
    4

    $ npm install express --save
    5'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.

    • 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. We need to require HTTP in our server to be able to use it. This can be done simply by adding the code below to our server.

      var http = require('http');
    • Phương pháp 2 (sử dụng giao diện HTTP): Mặc dù phương pháp đầu tiên là đủ cho hầu hết các giải pháp, có một phương thức khác sử dụng giao diện HTTP bằng Node.js và trả về dữ liệu JSON. Node.js đi kèm với một mô-đun HTTP được xây dựng, vì vậy chúng tôi đã giành chiến thắng để cài đặt nó một cách riêng biệt như chúng tôi đã làm cho Express. We will now use http.createServer() and JSON.stringify() to return JSON data from our server.

    Example:

    index.js

    Bướ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 --save
    7const app = express();2const app = express();3
    $ npm install express --save
    2const app = express();5);

    $ npm install express --save
    7const app = express();8
    node index.js
    3
    $ npm install express --save
    00

    const express = require('express');
    const app = express();
    0

    $ npm install express --save
    02

    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.

    Hướng dẫn nodejs return json - nodejs trả về json

    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.