CORS, Cross-Origin Resource Sharing: Allowing Cross-Domain Communication

Cross-Origin Resource Sharing (CORS) is an essential mechanism that enables communication between clients and servers, even if they are on different domains. Normally, JavaScript applications running in the browser can only access HTTP resources on the same domain that serves them. However, CORS provides a way to allow connections to other servers. By default, certain resources like images, scripts, and styles can be loaded from different origins. However, requests made using XHR or Fetch to a different domain, subdomain, port, or protocol will fail unless the server implements a CORS policy....

Express Middleware: Enhancing Your Routing Process

Express middleware plays a crucial role in the routing process. By creating custom functions, we can insert them at different points in the chain to perform specific operations. Typically, middleware functions are used to modify the request or response objects or terminate the request before it reaches the route handler. These functions are added to the execution stack using the app.use() method, similar to defining a route. For example: app.use((req, res, next) => { /* middleware function */ }) In the above code snippet, we define a middleware function that takes three parameters: req (request object), res (response object), and next....

Express Templates: A Guide to Server-Side Template Engines

Express is a powerful framework that can handle server-side template engines, allowing developers to dynamically generate HTML by adding data to views. The default template engine used by Express is Jade, which has now been renamed to Pug due to a trademark issue. While Jade (or Pug 1.0) is still the default in Express for backward compatibility reasons, it’s recommended to use Pug 2.0 or another engine of your choice in new projects....

Express: Request Parameters

A comprehensive guide to the properties of the Request object and how to use them effectively. Request Parameters The Request object in Express holds crucial information about the HTTP request. Here are the key properties you’ll likely use: Property Description .app Holds a reference to the Express app object. .baseUrl Represents the base path on which the app responds. .body Holds the data submitted in the request body. However, it must be parsed and populated manually before accessing it....

How to Retrieve the GET Query String Parameters using Express

Understanding how to retrieve the query string parameters from a GET request is crucial when building web applications with Express. The query string is the part of the URL that comes after the path and starts with a question mark “?”. Here’s an example of a query string: ?name=flavio Multiple query parameters can be added using “&”: ?name=flavio&age=35 So, how do you retrieve these query string values in Express? Express simplifies this task by populating the Request....

How to Setup Let's Encrypt for Express and Enable HTTPS

If you run a Node.js application on your own VPS, you’ll need a solution for obtaining SSL certificates. In this tutorial, we will guide you on how to set up HTTPS using the popular free solution Let’s Encrypt and Certbot. These are the steps we’ll follow: Install Certbot Generate the SSL certificate using Certbot Allow Express to serve static files Confirm the domain Obtain the certificate Setup the renewal Install Certbot To install Certbot on a Linux distribution that uses apt-get to manage packages, run the following commands:...

How to Upload Files to the Server Using JavaScript

Uploading files and processing them in the backend is a common functionality in web applications, such as uploading avatars or attachments. In this article, we will learn how to upload files to the server using JavaScript. Uploading Files Client-Side To enable file upload functionality in our web app, we start by adding an HTML file input element: <input type="file" id="fileUpload" /> Next, we register a change handler on the #fileUpload DOM element....

Managing file uploads in forms using Express

Learn how to handle and store files uploaded via forms in Express. This is an example of an HTML form that allows users to upload files: <form method="POST" action="/submit-form" enctype="multipart/form-data"> <input type="file" name="document" /> <input type="submit" /> </form> Don’t forget to include enctype="multipart/form-data" in the form, otherwise files won’t be uploaded. When the user presses the submit button, the browser will send a POST request to the /submit-form URL on the same origin of the page....

Processing Redirects in Express: A Guide for Server-side Redirection

Redirects play a crucial role in web development by allowing you to efficiently direct users to other pages. In this guide, we will explore how to handle redirects using Express, a popular web application framework for Node.js. To begin with, you can initiate a redirect using the Response.redirect() method. Let’s take a look at a simple example: res.redirect('/go-there'); By using this code snippet, a 302 redirect will be created, indicating a temporary redirect....

Sanitizing input in Express using express-validator

In the world of running a public-facing server, it’s crucial to never trust the input you receive. Even though you may have implemented client-side code to sanitize and block any weird input, there are still ways for people to manipulate and exploit your server. That’s why it’s important to sanitize your input. Luckily, the express-validator package that you already use for input validation can also be used for sanitization. Let’s say you have a POST endpoint that accepts parameters like name, email, and age:...