InterviewSolution
| 1. |
How to get the full URL in Express.js app? |
|
Answer» Sometimes we need to get the full URL in the server and we can do it through the express app. We will look into the same in this EXAMPLE. We will first go through the basic setup step and 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 our app by giving npm i express from the command line. Now, create a file server.js and add the below code in it. Here, we have an app.get(), which is LOOKING at all routes. Here, from the request, we are first getting the protocol which is basically the type of request - http or https. Next, we are getting the host which can be any address on the internet or localhost. We are also getting the URL, which is basically the part which comes after the localhost. Next, we are also getting the port on which the request is coming. After that we are just constructing the full URL, using all the parameters and SENDING back to the client. Now, start the server with node server.js in the command line. After that go to any url and, we will get the complete URL printed. |
|