Server Side Rendering with Node, Express & Cookies
Introduction
You are very familiar with JavaScript by now, and you may notice that we can only run JavaScript in our browser using the <script>
tag. But what if we want to write scripts using JavaScript and run them directly on our local machines, just like how we do with Python?
This is where Node.js comes into play. Node.js is a JavaScript runtime engine built on Chrome's V8 JavaScript engine that allows you to run JavaScript outside of the browser. This means you can do a lot more with JavaScript, such as managing local files, setting up a server, and much more. Essentially, it lets JavaScript move beyond the browser, enabling server-side development.
Learning Objectives
By the end of this lesson, you should be able to:
- Understand how npm (Node Package Manager) and Node.js work together.
- Create backend APIs using Node.js and Express.
- Make API calls using Axios.
- Work with the file system using Node.js and Express.
- Understand and use cookies to manage state and data in your applications.
Getting Started
Let's start by creating a folder to organize our code. We’ll call this folder example
and navigate into it using the console:
mkdir example
cd example
NPM (Node Package Manager)
With Node.js, we gain the ability to run JavaScript locally on our machines. Along with this, we benefit from a rich ecosystem of packages contributed by a large community of developers. The possibilities for JavaScript become endless!
To manage the many packages that give Node.js its powerful abilities, we use npm. Node Package Manager (npm) is where you can find and download Node packages that add extra functionality to your projects.
Installing Node.js
To get started, you need to install Node.js on your computer. Visit https://nodejs.org/en/ and download the installer for your operating system.
After installation, you can confirm that Node.js and npm were installed correctly by typing the following commands in your terminal:
node -v
npm -v
If both commands return version numbers, you're good to go!
Now, let’s install our first package. Throughout this example, we’ll use Axios to make HTTP requests, so let’s install Axios using npm.
Navigate to the folder where we are storing our project (in this case, example/
), and type the following commands:
npm init -y
npm install axios
- The
npm init
command is used to create a new npm package. Essentially, every Node.js project can be treated as an npm package. - The
y
flag skips all configuration steps and creates a basic npm package with default settings.
After running these commands, you should see the following files in your example/
directory:
node_modules
folder: This is where your installed packages are stored.package.json
file: This file keeps track of your project’s dependencies and settings.package-lock.json
file (optional): This file ensures that all developers working on the project use the exact same package versions.
The package.json
file is especially important as it defines the top-level packages and dependencies for your project.
Hello World with Node.js
Now, let’s try running a simple Node.js script. Create a file called helloworld.js
in the example/
folder and add the following code:
console.log("Hello, world.");
To run this script, use the following command in your terminal:
node helloworld.js
You should see "Hello, world." printed in the terminal. This simple example shows how you can run JavaScript code outside of a browser using Node.js.
JavaScript in the Browser vs. Node.js
JavaScript in the browser and JavaScript in Node.js share the same language syntax, but there are key differences that you need to be aware of:
- Modules: In Node.js, you use the
require
function to import modules (packages or files), whereas in the browser, you typically use<script>
tags or ES6import
statements. - No
document
object: Since Node.js runs outside the browser, it doesn't have access to thedocument
object or the DOM (Document Object Model). This means you can’t directly manipulate HTML elements. - File System Access: Node.js can interact with the local file system, allowing you to read, write, and manipulate files—a feature not available in browser-based JavaScript.
- No
fetch
function: In Node.js, there's no built-infetch
function to make HTTP requests. Instead, you can use thehttp
module or third-party packages like Axios to handle HTTP requests.
Understanding these differences is crucial as you start working with Node.js to build server-side applications.
SSR - Server-Side Rendering
So far, we've been working with JavaScript and HTML files directly in our browser. You might have just double-clicked on your index.html
file to open it in your browser. While this is convenient, it's not how real websites work!
How Do Websites Really Work?
In a real-world scenario, a special program called a server is constantly running on a computer somewhere on the internet. This server listens for incoming requests from users and responds by sending back HTML files (and other resources like CSS, JavaScript files, etc.).
What is the Internet?
The internet is essentially a network of computers, each with a unique IP address, that communicate with each other to send and receive information. Think of it like a massive system of interconnected roads, where each computer is a house with its own unique address.
Let's say you have a friend named Sara who lives across town. You want to send her a letter, so you write down her address on an envelope and drop it in the mailbox. The postal service picks it up, reads the address, and delivers the letter to Sara's house. This is similar to how information is sent across the internet.
Accessing the Server
Imagine Sara wants to share her holiday photos with you, but instead of mailing them, she decides to create a website. She sets up a computer in her home, and this computer will act as a server. The server’s job is to host the website and respond to requests from people who want to see the photos.
To visit Sara’s website, you need to know her computer’s IP address, which is like knowing her home address. When you type this IP address into your browser, it’s like sending a request to her computer, saying, “Please show me the holiday photos.”
However, Sara’s computer won’t respond to just any request. To make sure your request reaches the right program on her computer (the web server), you also need to specify the correct port. A port is like a specific door to a room in Sara’s house where the photos are stored. Web servers commonly use port 80 for HTTP requests.
You can tell your browser to communicate with Sara’s server on port 80 by typing something like 192.168.1.10:80
into the URL bar. When you hit enter, your computer sends a request over the internet to Sara’s computer, specifying that it wants to access the server through port 80. Sara’s server then receives this request, processes it, and sends back the HTML page containing her holiday photos, which your browser displays.
This is how you access websites on the internet. Behind the scenes, your request travels across a series of connected networks to reach the correct server, and the server sends the information back to your browser to display the webpage.
Configuring Your Network
Unfortunately, there are not enough IP addresses for every device in the world. To get around this, most home networks use a router, which has a public IP address. Devices connected to the router get private IP addresses, which are used within the local network.
If you want to host a server on your computer and make it accessible to others, you might need to configure your router to allow incoming requests to reach your computer. This involves setting up port forwarding on your router and possibly adjusting your firewall settings to allow incoming connections.
Alternatively, you can rent a server from a cloud provider, which handles all of this configuration for you.
Domains
Remembering an IP address can be challenging, so we use domain names (like vikhyath.com
) that map to IP addresses. When you type a domain name into your browser, your browser contacts a DNS server (a special server that stores mappings between domain names and IP addresses) to find the correct IP address.
Once your browser knows the IP address, it can send the request to the correct server.
Default Port
Browsers usually default to using port 80 for HTTP requests, so if you don’t specify a port, the browser will automatically send the request to port 80. For example, typing google.com
in your browser is the same as typing google.com:80
.
Web Server with Node.js & Express
Now that we understand the basics of how the internet and servers work, let’s build our own web server using Node.js and Express.