Chapter 7. Networking, Sockets, and Security

The core of a Node application invariably relies on two primary infrastructure components: networks and security. And you can’t talk about networks without also discussing sockets.

I group networks and security because once you start moving beyond a single, isolated machine, security should be paramount in your mind at all times. Every time you finish a new piece of the application, the first question you should ask yourself is: is this secure? No amount of graceful coding can compensate for letting crap in.

Servers, Streams, and Sockets

Much of the Node core API is related to creating services that listen to specific types of communications. In Chapters 1 and 5 we used the HTTP module to create web servers listening for HTTP requests. Other modules can create a Transmission Control Protocol (TCP) server, a Transport Layer Security (TLS) server, and a User Datagram Protocol (UDP) socket. I’ll cover TLS later in the chapter, but in this section I want to introduce the TCP and UDP Node core functionality. First, though, a quick look at sockets.

Sockets and Streams

socket is an endpoint in a communication, and a network socket is an endpoint in a communication between applications running on two different computers on the network. The data flows between the sockets in what’s known as a stream. The data in the stream can be transmitted as binary data in a buffer, or in Unicode as a string. Both types of data are transmitted as packets ...

Get Learning Node, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.