DEV Community

Nikhil Singh Rajpoot
Nikhil Singh Rajpoot

Posted on

“Stop Putting login.js in the Wrong Place — Here’s the Industry Standard”

checkout my repo ---->
As a backend-focused developer diving deeper into fullstack architecture, I recently explored a deceptively simple question: Where should I place my login.js file in a React + Node.js project?

Turns out, the answer reveals a lot about how modern web apps are structured. Here's what I learned

Frontend: UI Lives in pages/
In React projects, the login screen is a visual component, not a route handler. So it belongs in:

Code
src/pages/Login.js

This component handles:

Rendering the login form

Capturing user input

Sending credentials to the backend via axios or fetch

Example:
jsx

<form onSubmit={handleLogin}>
  <input type="email" ... />
  <input type="password" ... />
  <button type="submit">Login</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Backend: Logic Lives in routes/ and controllers/
The backend doesn’t render the login page — it processes login requests. So the logic goes here:

Code
/routes/auth.js ← defines POST /login

/controllers/authController.js ← contains loginUser(req, res)
Example:
js

exports.loginUser = (req, res) => {
  const { email, password } = req.body;  // Validate user, return token
};
Enter fullscreen mode Exit fullscreen mode

What About views/ in Backend?
This was my biggest “aha” moment: In React-based projects, you don’t need backend views at all.

Backend views (like EJS, Pug, Handlebars) are used in server-rendered apps. But in a React SPA:
.The frontend renders all UI

.The backend only serves JSON data

.views/ folder is unnecessary

Summary :-
Role Handled By Folder Used
UI Rendering React src/pages/
Auth Logic Express routes/, controllers/
HTML Templates Not used views/ (skip it)

Final Thoughts :-
This small architectural decision — where to place login.js — taught me how clean separation between frontend and backend improves scalability, clarity, and maintainability. If you're building a fullstack app, understanding this split is essential.

Top comments (0)