A clean starter for building server-rendered auth flows with EJS and MongoDB. Includes registration, login, cookie-based JWT sessions, and a simple home page.
- Express + EJS: Fast server-rendered pages (
views/) - MongoDB (Mongoose): Simple
Usermodel (models/user.model.js) - Auth Ready: Register, Login, JWT in cookies
- Dotenv: Environment-based configuration
├─ app.js # App entry
├─ config/db.js # MongoDB connection
├─ models/user.model.js # User schema
├─ routes/
│ ├─ index.route.js # Home/Index routes
│ └─ user.route.js # Auth (register/login)
└─ views/ # EJS templates
├─ home.ejs
├─ index.ejs
├─ login.ejs
└─ register.ejs
- Node.js 18+
- MongoDB running locally or a cloud URI
- Install dependencies
npm install- Create a
.envfile in the project root
MONGO_URI=mongodb://localhost:27017/drive-app
JWT_SECRET=your-super-secret-key- Start the server
npm start # production
# or
npm run dev # with nodemonThe app runs at http://localhost:3000.
- GET
/→ Index page - GET
/user/register→ Registration page - POST
/user/register→ Create user (validatesusername,email,password) - GET
/user/login→ Login page - POST
/user/login→ Authenticate; setstokencookie
- express, ejs, mongoose
- bcrypt, jsonwebtoken, cookie-parser
- express-validator, dotenv
{
"start": "node app.js",
"dev": "nodemon app.js"
}- JWT is stored in a cookie named
tokenafter login. - The
Usermodel trims fields and validates lengths; passwords are hashed with bcrypt.
- Add logout route (clear the
tokencookie) - Protect pages using a JWT-check middleware
- Add flash messages and better form UX in EJS views
MIT