Linn Linn Htun
Linn Linn Htun
AvatarLinn Linn Htun

Web Servers with Node.js

October 29, 2023

Web Servers with Node.js

Node.js is an ideal platform for building web servers thanks to its non-blocking I/O model and the rich npm ecosystem. This post covers building web servers using Node.js's built-in http module, then with Express.js for more powerful routing and middleware support, handling HTTP requests and responses, and serving static files.

What is a Web Server?

Node can be used to write programs that do just about anything, from parsing files and exploring astronomical data to even making games. But whatever we're making, Node is almost always used to bring our programs to the web by building the backend for websites and mobile applications that share the data for our astronomical observations, games, or whatever it is that we're building. 

Let's be clear, what is a backend, a web server, or an HTTP server? And how does everything work? When you type something into your browser, say you're going to a site facebook.com, your browser actually starts by calling a system called DNS or the domain name system. It uses DNS by talking to DNS servers across the internet.  These servers look up the internet address of the server, given the name and the URL that we've typed into our browser.

 

 

Introduction to HTTP Response and Requests

We can make requests to our HTTP web server to get data back from it. The thing that defines how our web server responds to these requests is our API. The API tells us what kinds of functions the server should support and how those functions should be used. Functions like, for example, getting a list of your friends or getting their messages or photos. We can implement our API on the server in Node, in Python, or any other programming language. 

What matters is that the language that we use when reacting to these requests and responding to them is HTTP, which is the common way that the browser and the server can use to understand what both sides are seeing.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

 

HTTP Requests

What kinds of messages do we send back and forth in HTTP? What does an HTTP conversation look like? API stands for application programming interface. It specifies how two applications talk to each other. If you think of an API almost like a set of phrases in a language that you can put together to communicate an idea or to get some work done, then it's generally the browser that starts that conversation and asks questions of the server waiting for its response before potentially making another request. It's almost like the browser is interviewing the server. 

So what kinds of things can we ask of our web server? Well, with HTTP, our API is made up of operations using methods like get and post, which we saw are used on different collections of data, for example, a list of friends or messages or photos. We can combine the name of the method with the collection name to get the data that the server has on who our friends are. 

https://www.mulesoft.com/resources/api/what-is-an-api

 

What is an ORIGIN?

When we browse the Web and we go to a site like Google Maps, what we type in the browser is something like

eg. https://www.google.com/maps

The origin is a combination of three things in what you see here. The first is the protocol, which is the part that says how you're communicating with the server at Google, in this case using the HTTP secure protocol, next up, we have a critical part of the origin, which is the host. 

eg. https://www.google.com/maps

The Host tells us which server we're going to be browsing to or which server is going to be handling our requests.

 And third part of the origin is the ports.

eg. https://www.google.com:443/maps

Whenever it's included in your request. And this would look something like :443, which is the port that your browser assumes when you're using the secure HTTP protocol. 

 

Whenever one of the above three things changes, we are no longer on the same origin. We can browse to other pages at that origin, so maybe we replace maps with mail to get our email, but we can't change google.com to facebook.com and still be at the same origin. That makes a lot of sense.

 

What is CORS?

CORS stands for cross origin resource sharing.  it's a way of relaxing the restrictions that the same origin policy puts on us developers. So that we can make applications that potentially span many different domains and origins because the same origin policy generally limits us in the browser to take to just one origin. 

 

Frequently Asked Questions

How do you create a web server in Node.js?

You can create a basic web server in Node.js using the built-in http module with http.createServer(), or use the Express.js framework for a more feature-rich experience with routing, middleware support, and a cleaner API.

What is middleware in Express.js?

Middleware functions in Express are functions that have access to the request and response objects and the next middleware function in the pipeline, used for tasks like logging, authentication, body parsing, and error handling.