0

Our Node JS server uses net module for tcp/ip communication, is it possible use net module to send http response? Do I have to use http module to send http response?

2 Answers 2

5

I'd like to add some extra information about what exactly is required to send an HTTP response using regular socket programming.

Having written HTTP servers at least 4 times in my life (first time for fun then later for work) I'd like to point all those who are interested to figure out the minimum effort required to implement a standards compliant HTTP response to James Marshall's excellent HTTP Made Really Easy. The page has existed since 1998 and to this day remains one of my favourite reference about what RFC 2616 (the HTTP spec) really requires and what can be left out.

I've written my own HTTP server in node once before because I needed custom processing that the http module doesn't provide (or I just couldn't figure out how to do). So this knowledge is still useful.

The absolute minimum requirement for an HTTP response is:

let server = net.createServer(sock => {
    let client = sock.remoteAddress;
    console.log('serving stream to ' + client);

    // Headers:
    sock.write(
        'HTTP/1.0 200 OK\r\n' +
        '\r\n'
    );

    sock.write('Hello world');
    sock.close(); // HTTP 1.0 signals end of packet by closing the socket
});

server.listen(SOME_PORT);

From here you can modify the code any way you want to process the HTTP request however you need to.

In general it is often easier to use something like the http module or something higher level like the connect framework or Express.js but knowing this trick is sometimes helpful.

That one time I wrote my own http server using the net module was because I needed to implement a streaming multipart server so that I can stream an mjpeg video stream. The http module assumes a request/response model which does not fit with the connection/stream model I needed

Sign up to request clarification or add additional context in comments.

Comments

4

One can send an http response over a plain TCP socket as that is exactly what the http module itself does (it's built on top of the net module). But, it requires that you write your own code that follows the http protocol for connection, receipt of request and sending of response.

But, what you're asking doesn't really make sense. Every TCP socket needs some protocol (even if its a custom protocol) for the two ends to know how to communicate. So, if you're saying the protocol is http and thus you need to send an http response, then you may as well use the pre-built http module rather than rebuilding all that yourself.

If, for some reason, you have some other protocol on a TCP socket and now you want to send just an http response over that socket, you will have to find or write your own code to build an http response that follows all the proper http rules. It can be done - it's just work and a lot of detail understanding the specification so you make sure you have all the rules and special cases covered.

Our Node JS server uses net module for tcp/ip communication, is it possible use net module to send http response?

Yes it's possible, that's what the http module does.

Do I have to use http module to send http response?

You don't have to use the http module. It's just code that creates an http response and follows all the http specifications. You can write your own code that does something similar if you really want to. The point of the http module is to allow you to build on top of all that prewritten code rather than re-implementing it all yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.