Express.js is a popular Node.js web application framework known for its simplicity and flexibility. It allows developers to build powerful web applications quickly and easily. One essential aspect of web development is handling cookies, which are small pieces of data that can be stored on a user's computer. In this blog post, we will explore how to use Express.js middleware to handle cookies effectively.
What are Cookies?
Cookies are key-value pairs that are sent from a web server and stored on a user's device. They are commonly used for various purposes, such as session management, user authentication, and tracking user preferences. Cookies are an essential part of web development, especially when it comes to maintaining user sessions and personalizing user experiences.
Setting Up an Express.js Application
Before we dive into cookie handling, let's set up a basic Express.js application. Make sure you have Node.js installed on your machine, and then create a new directory for your project. Inside the project directory, run the following commands to initialize a new Node.js project and install the required dependencies:
npm init -y
npm install express cookie-parser
Now, create a JavaScript file (e.g., app.js
) and add the following code:
// Import required modules
import express from "express";
import cookieParser from "cookie-parser";
// Create an Express app
const app = express();
// Middleware to parse cookies
app.use(cookieParser());
// Define a route to set a cookie
app.get("/set-cookie", (req, res) => {
// Set a cookie named "user" with a value "John"
const sixDays = 6 * 24 * 60 * 60 * 1000;
res.cookie("user", "John", { maxAge: sixDays });
res.send("Cookie set!");
});
// Define a route to read the cookie
app.get("/get-cookie", (req, res) => {
// Retrieve the value of the "user" cookie
const userCookie = req.cookies.user;
if (userCookie) {
res.send(`Hello ${userCookie}!`);
} else {
res.send("Cookie not found!");
}
});
// Start the Express server
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
This code sets up a basic Express.js server and defines two routes: one for setting a cookie and another for reading the cookie. We use the cookie-parser
middleware to handle cookies in our application.
Setting a Cookie
In the /set-cookie
route, we set a cookie named "user" with the value "John" and a maximum age of six days. This means the cookie will expire and be deleted from the user's device after six days.
Reading a Cookie
In the /get-cookie
route, we retrieve the value of the "user" cookie and check if it exists. If the cookie is found, we send a personalized greeting; otherwise, we inform the user that the cookie is not found.
Conclusion
In this blog post, we've covered the basics of using Express.js middleware to handle cookies in your web applications. Cookies are a fundamental part of web development and are used for various purposes, including session management and user authentication. Express.js simplifies cookie handling with middleware like cookie-parser
, making it easy to work with cookies in your applications.`