Node.js HTTP/2 server: obtain request method

In this tutorial we will check how to obtain the method of a request performed to a HTTP/2 Node.js server.

Introduction

In this tutorial we will check how to obtain the method of a request performed to a HTTP/2 Node.js server.

Note that we have already covered how to setup a very simple HTTP/2 webserver with Node.js on this previous tutorial.

The mentioned tutorial explains in more detail how to generate the private key and certificate files needed to setup the server. For this tutorial, I’m assuming that you have already generated these files.

We will use a web browser with HTTP/2 support as a client. Since we will simply access an endpoint on the server, the browser will send a GET request, which is the method we should then expect to obtain in our code.

The tests from this tutorial were done on a Windows 8.1 machine.

The code

We will start our code by the modules import. We will importe the HTTP/2 module, which exposes all the functionality we will need to setup the server, and the file system module, so we can read the content of the certificate and private key files.

const http2 = require('http2');
const fs = require('fs');

After this, we will define the callback function that will be executed when a request is received by the server. When it happens, the function will be invoked with two parameters: an object of class Http2ServerRequest and an object of class Http2ServerResponse.

As we saw on the previous tutorial, the first object is used to access to information of the request and the second allows to send a response back to the client.

In our case, we will use the Http2ServerRequest object to obtain the method used by the client and then we will use the Http2ServerResponse object to send a response back.

function onRequest (req, resp) {
// callback function implementation
}

So, in the implementation of the callback function, we will obtain the method of the request by accessing the method property of the Http2ServerRequest object. We will log it’s value to the console.

console.log(req.method);

To send a response back to the client, we need to call the end method on the Http2ServerResponse object, passing as input the content. We will return a simple message to the client containing the method used on the request.

resp.end("You have used method " + req.method);

The full callback function can be seen below.

function onRequest (req, resp) {
  console.log(req.method);
  resp.end("You have used method " + req.method);
}

Now that we have defined the callback, we will create the server instance with a call to the createSecureServer function of the HTTP/2 module.

As first parameter we need to pass an object with two properties containing the private key and the server certificate. The properties of this object should be called key and cert, respectively.

We will obtain the content of both the certificate and private key files by calling the readFileSync function of the file system module.

This method receives as input the path to the file to read. In my case, since I have both files in the same directory of the code, I simply need to pass the filename.

The second parameter of the createSecureServer function should be the callback we have defined before.

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
}, onRequest);

To finalize, we need to call the listen method on our server instance. This method receives as input the number of the port where the server should listen for incoming requests. I’ll be using port 8443.

server.listen(8443);

The final code can be seen below.

const http2 = require('http2');
const fs = require('fs');

function onRequest (req, resp) {
  console.log(req.method);
  resp.end("You have used method " + req.method);
}

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
}, onRequest);

server.listen(8443);

Testing the code

To test the code, first run it on a tool of your choice, so the server starts listening to incoming client requests. I’ll be using Visual Studio Code with the Code Runner extension.

Then, open a browser of your choice with HTTP/2 support (I’ll be using Google Chrome) and access the following URL:

https://localhost:8443/

Note that, since we are using a self signed certificate, your browser should give a warning about security. Choose to advance.

You should get an output similar to figure 1, which illustrates the response returned by the server. As can be seen, it indicates that the server received a GET request, as expected.

HTTP/2 GET request performed to Node.js server, with Google Chrome

Figure 1 – Answer to the HTTP/2 GET request.

If you go to the console of the tool you are using, you should see the request method getting printed, as shown in figure 2. Note that even if you access only once to the endpoint, the code may output twice the method “GET“.

This happens because browsers typically do more than one request (for example, to try to fetch the website’s favicon).

Javascript HTTP2 server get request method.png

Figure 2 – Output of the program on the console.

Related Posts

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s