|
Javascript Object Notation (JSON) is a widely used format for sending and receiving data to or from the server. In this article, we will use fetch API to send and receive data from the NodeJS server. Advantages of JSON:
Why use Fetch API?
Setup and Installation Step 1: Create a folder named SendDataToClient and run the following command to initiate a NodeJS application. npm init -y Step 2: Install the necessary npm packages using the following command. npm install express body-parser Step 3: Create index.html and server.js in your Project directory. Project Structure: Now, our project directory will look like this. Frontend Code: The structure is straightforward, with two inputs: one for name and one for email. A send button to send the input data to the server and an empty paragraph to display the server’s JSON response. index.html
Backend Code: The Express framework with NodeJS is used to build the server, which runs on port 3000. Whenever the user clicks on send button it sends a POST request containing JSON data on /jsondata route and then the server responded with a JSON reply. server.js
Steps to Run the Application: To run the application using the following command: node server.js Output: Go to http://localhost:3000/ to see the output screen. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |