| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Β | Β | |||
Β | Β | |||
Β | Β | |||
A full-featured, secure URL Shortener RESTful Web Application built using Node.js, Express.js, MongoDB (Mongoose), and Server-Side Rendering (EJS). The project implements strict MVC Architecture, stateless JWT Authentication (via HTTP-Only Cookies), and Role-Based Access Control (RBAC).
| Tool / Library | Type | Purpose |
|---|---|---|
| Node.js | Runtime | JavaScript execution engine on server-side |
| Express.js | Framework | Fast, unopinionated web framework for Node.js |
| MongoDB | Database | NoSQL Document-based database |
| Mongoose | ODM Library | Schema-based modeling for MongoDB & Express |
| nodemon | Developer Tool | Automatically restarts the node application when file changes in the directory are detected |
| nanoid | Utility | Small, secure, URL-friendly unique string ID generator |
| EJS | Template Engine | Server-side rendering (SSR) of dynamic HTML templates |
| uuid | Utility | Cryptographically strong Universally Unique Identifier (UUID) generator (e.g., v4 for unique session/user IDs) |
| jsonwebtoken | Authentication | Generates and verifies digital signatures/tokens (JWT) to securely pass information between client & server |
| cookie-parser | Middleware | Express middleware to parse incoming request Cookie headers and populate req.cookies |
| dotenv | Utility | Loads environment variables from a .env file into process.env |
| Postman | API Client | Testing REST API endpoints (GET, POST, PATCH, DELETE) |
Generated: 8/2/2026, 1:35:39 PM Root Path: workspace\Node
βββ π Learning β βββ π Events β β βββ π main.js β β βββ π test.txt β βββ π Express β β βββ βοΈ .gitignore β β βββ π index.js β β βββ βοΈ package-lock.json β β βββ βοΈ package.json β βββ π First β β βββ π contacts.txt β β βββ π file.js β β βββ π file.txt β β βββ π math.js β β βββ π modules.js β β βββ βοΈ package.json β β βββ π test.txt β βββ π HTTP β β βββ π http-notes.js β βββ π RestAPI β β βββ π controllers β β β βββ π user.js β β βββ π logs β β β βββ π logs.txt β β βββ π middlewares β β β βββ π middleware.js β β βββ π models β β β βββ π user.js β β βββ π routes β β β βββ π user.js β β βββ π views β β β βββ βοΈ .gitkeep β β βββ βοΈ .gitignore β β βββ βοΈ MOCK_DATA.json β β βββ π connection.js β β βββ π index.js β β βββ βοΈ package-lock.json β β βββ βοΈ package.json β βββ π Server β β βββ π index.js β β βββ π log.txt β β βββ βοΈ package.json β β βββ π url_note.js β βββ π Versions β βββ π version.js βββ π Shortener_URL β βββ π controllers β β βββ π static.js β β βββ π url.js β β βββ π user.js β βββ π middlewares β β βββ π auth.js β βββ π models β β βββ π url.js β β βββ π user.js β βββ π routes β β βββ π static.js β β βββ π url.js β β βββ π user.js β βββ π services β β βββ π auth.js β βββ π views β β βββ π home.ejs β β βββ π login.ejs β β βββ π signup.ejs β βββ βοΈ .gitignore β βββ π app.js β βββ π connect.js β βββ βοΈ package-lock.json β βββ βοΈ package.json β βββ βοΈ vercel.json βββ π README.md
In the project directory, you can run the following commands configured in package.json:
npm startBefore deploying, ensure your database access is open to Vercel:
Set up your environment variables in Vercel before or after deployment:
π‘ Note: If you edit or update any Environment Variable after deploying, always go to the Deployments tab, click the three dots (...) on the latest deployment, and select Redeploy to apply changes! & Make sure your project structure strictly follows the root layout:
Shortener_URL/
βββ models/
βββ routes/
βββ middlewares/
βββ views/
βββ .env
βββ app.js
βββ connect.js
βββ package.jsonDownload and install the LTS version from Node.js Official Site. Verify installation in your terminal:
node -v
npm -v# Verify connection using mongosh CLI
mongoshInitialize your Node project inside any project directory:
npm initpackage name: (name)
version: (1.0.0)
description: may add β entry point: (index.js)β
test command: just enterβ
git repository: (https://github.com/username/repo_name.git)
keywords: may add
author: may add
license: (ISC)
type: (commonjs) recommended module
Is this OK? (yes)
To enable modern ES6 import/export syntax instead of require(), open package.json and add "type": "module":
{
"name": "node",
"version": "1.0.0",
"bugs": {
"url": "https://github.com/username/repo/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/username/repo.git"
},
"license": "ISC",
"author": "",
"type": "commonjs",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js" // add manually write "nodemon index.js" if installed
}
}
Run this command inside your project directory to install npm packages in the package.json file:
npm i express
npm i mongoose
npm i nanoid
npm i nodemon
npm i ejs
npm i cookie-parser
npm i uuid
npm i jsonwebtoken
npm i dotenvIt automatically creates package.json if not exist. Like
{
"name": "pakage name",
"version": "1.0.0",
"description": "Something",
"keywords": [
"keywords"
],
"homepage": "https://github.com/github.com/username#readme",
"bugs": {
"url": "https://github.com/github.com/username/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/github.com/username.git"
},
"license": "ISC",
"author": "Author",
"type": "module",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"dependencies": {
"cookie-parser": "^1.4.7",
"dotenv": "^17.4.2",
"ejs": "^6.0.1",
"express": "^5.2.1",
"jsonwebtoken": "^9.0.3",
"mongoose": "^9.8.0",
"nanoid": "^6.0.0",
"nodemon": "^3.1.14",
"uuid": "^14.0.1"
}
}
Note: Install packages when needed
| Component / Module | Purpose |
|---|---|
| index.js | Main entry point; configures dotenv, EJS view engine, global middlewares (cookie-parser, express.json), DB connection, and routes. |
| connect.js | Handles async database connection setup using Mongoose to connect with MongoDB. |
| models/url.js | Mongoose schema for short links; stores shortId, redirectURL, visitHistory, and reference to createdBy user. |
| models/user.js | Mongoose schema for authentication; manages user details including name, email, password, and role (Normal, Admin). |
| controllers/url.js | Core URL business logic; generates unique shortIds via nanoid and logs click timestamps upon redirection. |
| controllers/user.js | Handles user authentication logic (HandleUserSignup, HandleUserLogin) and sets JWT cookies. |
| services/auth.js | Pure JWT token manager; encodes user payload on login (setUser) and decodes/verifies incoming tokens (getUser). |
| middlewares/auth.js | Security layer (AuthorizationHeaderVal, RestrictTo); extracts JWT cookies and enforces role-based access control. |
| routes/url.js | Protected URL endpoints (POST /url); restricted to authenticated roles (Normal, Admin). |
| routes/static.js | Handles SSR page rendering for home dashboard (/), signup (/signup), and login (/login) views. |
| routes/user.js | Endpoints for authentication workflows (POST /user/signup, POST /user/login). |
| views/ | Contains EJS template files (home.ejs, signup.ejs, login.ejs) for rendering the user interface. |
π‘ Note: Postman automatically manages the HTTP-only token cookie after login.
{
"name": "MehmoodCoder",
"email": "mehmood@example.com",
"password": "Password123"
}{
"email": "mehmood@example.com",
"password": "Password123"
}{
"url": "[https://github.com/MehmoodCoder](https://github.com/MehmoodCoder)"
}{
"id": "8xK9p2Lm"
}If you need to clear all test documents without dropping the collection structure:
# 1. Open mongosh CLI
mongosh
# 2. Show All DB's
show dbs
# 3. Switch to project DB
use project-db-name
# 4. Show Exist DB collections
show collections
# 5. Show All Documents
db.collection.find({})
# 6. Delete all documents inside 'project-db-name' collection if wants
db.project-db-name.deleteMany({})
# 5. Exit the interactive MongoDB Shell session and return to the system terminal
.exit
# 6. And many more
MehmoodCoder
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.
| Back | FazBrowse Home | New Git URL |