How To Create Login Page In Node Js Using Mongodb

Welcome to my blog post on how to create a login page in Node.js using MongoDB! As a developer, I often find myself needing to implement user authentication functionality in my web applications. In this article, I will guide you through the process of building a login page using Node.js and MongoDB, sharing my personal insights and experiences along the way.

Setting Up the Environment

Before we dive into the code, let’s make sure we have everything we need to get started. First, you’ll need to have Node.js and MongoDB installed on your machine. If you haven’t already done so, head over to their official websites to download and install the latest versions.

Once you have Node.js and MongoDB installed, open your terminal and navigate to the root directory of your project. Run the following command to initialize a new Node.js project:

$ npm init

This command will create a package.json file, which will allow us to manage our project dependencies and scripts. Follow the prompts to set up your project details.

Installing Required Packages

Now that we have our project set up, we need to install the necessary packages to work with Node.js and MongoDB. Open your terminal and run the following commands:

$ npm install express
$ npm install mongodb

The express package will help us handle the HTTP requests in our Node.js server, while the mongodb package will allow us to interact with our MongoDB database.

Creating the Login Page

Now that we have all the necessary dependencies installed, let’s move on to creating our login page. In your project directory, create a new file called login.js. This file will be responsible for defining the routes and logic for our login page.

In login.js, start by requiring the necessary modules:

const express = require('express');
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const app = express();

Next, set up a connection to your MongoDB database:

const url = 'mongodb://localhost:27017';
const dbName = 'mydb';

Replace mydb with the name of your database. If you don’t have one yet, you can create a new database using the MongoDB shell.

Now, let’s define the route for our login page:

app.get('/login', (req, res) => {
res.send('Welcome to the login page!');

This route will handle GET requests to the /login URL and send a simple welcome message to the client.

Adding Form Markup and Styling

Let’s enhance our login page by adding a form for users to enter their credentials. In login.js, update the app.get('/login') route to serve an HTML file instead of a plain text response:

app.get('/login', (req, res) => {
res.sendFile(__dirname + '/login.html');

Create a new file called login.html in the same directory as login.js. Inside login.html, add the following markup:

<!DOCTYPE html>
<title>Login Page</title>
<link rel="stylesheet" href="login.css">
<h1>Login Page</h1>
<form action="/login" method="POST">
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Log In</button>

Save the file and create a new file called login.css in the same directory. Inside login.css, add some basic styling to make our login page visually appealing:

body {
font-family: Arial, sans-serif;
background-color: #f1f1f1;
margin: 0;
padding: 0;
h1 {
text-align: center;
margin-top: 50px;
form {
max-width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
input, button {
display: block;
width: 100%;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
button {
background-color: #4CAF50;
color: #fff;
cursor: pointer;

Save the file and restart your Node.js server. Visit http://localhost:3000/login in your browser to see your login page in action!


In this article, we explored how to create a login page in Node.js using MongoDB. We started by setting up the environment and installing the necessary packages. Then, we created a login page with a form for users to enter their credentials. We also added some basic styling to make the page visually appealing.

Remember, this is just the beginning. Building a secure and robust login system involves additional considerations, such as password hashing, session management, and input validation. However, I hope this article provided you with a solid starting point and sparked your curiosity to explore further.

Thank you for reading, and happy coding!