Save ID in Web Page and MongoDB using HTML, CSS, JS, Node.js and MongoDB
Introduction
Storing user credentials like ID and password is a foundational feature in any web application. Whether you're building a login system, a sign-up form, or a small data collection tool, securely capturing this data and storing it in a database is essential.
In this blog post, we'll walk through the complete process of building a full-stack app using:
HTML/CSS/JS for the frontend
Node.js and Express for the backend
MongoDB for the database
This project will allow users to input an ID and Password, display it on the web page, and store it in MongoDB for persistent storage.
Table of Contents
Prerequisites
Project Overview
Setting up the Project
Creating the Frontend (HTML, CSS, JS)
Building the Backend with Node.js and Express
Connecting to MongoDB
Storing and Displaying Data
Testing the App
Best Practices
Conclusion
1. Technologies Used
HTML5
CSS3
JavaScript (ES6)
Node.js
Express.js
MongoDB
Mongoose
2. Project Overview
We’ll build a web app where:
User enters an ID and Password in an HTML form
The submitted data is:
Displayed dynamically on the page
Sent to the server
Stored in a MongoDB database
3. Setting Up the Project
Step 1: Create Folder Structure
Step 2: Folder Structure
4. Frontend (HTML, CSS, JS)
public/index.html
CSS: style.css
JavaScript: script.js
5. Building the Backend with Node.js and Express
server.js
6. MongoDB Model
7. Storing and Displaying Data
When the user submits the form:
Frontend shows the ID and Password below the form
JS sends a POST request to
/submit
Server receives and saves it to MongoDB
Response confirms saving
This way, you achieve both frontend display and backend storage.
8. Testing the App
Step 1: Start MongoDB
Step 2: Run the App
Open browser at http://localhost:3000
Try entering:
ID:
user001
Password:
abc123
Check:
Displayed on page
Saved in MongoDB (
db.user.find()
in Mongo shell)
9. Best Practices
Security: Never store passwords as plain text. Use hashing (e.g.,
bcrypt
) for real-world apps.Validation: Add server-side validation to avoid bad data.
Environment Variables: Use
.env
for sensitive data like DB URI.Modular Code: Split routes and DB logic into separate files.
Frontend Enhancements: Add form validation, animations, alerts.
10. Conclusion
You’ve just built a fully functional mini full-stack project using HTML, CSS, JavaScript, Node.js, and MongoDB. This app:
Takes user ID and password input
Displays it on the webpage
Sends it to a Node.js backend
Stores it in MongoDB
This structure is a great starting point for bigger projects like:
Login/Sign-up systems
Admin panels
Secure data entry apps