InterviewSolution
| 1. |
How to post a Query in Express.js? |
|
Answer» To send data from client browser to server, we have many methods. We can also pass the data as a query parameter using GET requests. But it is not advisable to send through GET because the data will be exposed in the query parameter in the browser link. To send data the POST request is created and we should send through it only. We are going to send the data through POST in express and going to learn about it next. We will first create a new Node.js application by creating a directory and changing to it. Then will initialize a package.json file with the npm init -y command. Next, we will install express in the app by giving the npm i express command. Now, create a file server.js and import express and give a port. After that we are using a middleware for json. It is REQUIRED because we are going to pass our data as JSON to the backend. Next, we will create a post request and pass the name and email in the BODY. We have also created a function .listen() to listen for the function in LOCALHOST port 8080. Now, for checking our POST request, we need to use a postman app. Here, we need to send the application header as JSON and in the body pass name and email as json objects. After that hit the Send button and the POST request will be SENT successfully to the server. |
|