How to Generate or Send JSON Data at the Server Side using Node.js ?
- When compared to XML, it is easier to read and write.
- JSON is little in size. A JSON string is approximately two-thirds the size of the identical data in XML.
- A broad range of browsers supports it.
- JSON saves data in arrays, making data transmission easier. While it is not easy in XML.
Setup and Installation:
Step 1: To start a NodeJS application, create a folder called SendJsonData and run the following command.
npm init -y
Step 2: Using the following command, install the required npm packages.
npm install express
Step 3: In your project directory, create a file called index.js.
Project Structure: Our project directory should now look like this.
Example: The server, which runs on port 3000, is built using the Express framework and NodeJS. You can see a JSON response is sent by the server on the / (home) route.
Step to Run the Application: Use the following command to launch the application: