🌐 Web Development Learnings: Modern Backend Essentials

Here’s an overview of some important backend development concepts and best practices that make modern web apps more efficient, secure, and maintainable:

ESM Syntax in Node.js
Instead of traditional require, using modern ES Modules (import/export syntax) in Node.js has become popular. To enable this, add "type": "module" in your package.json:

jsonCopyEdit{
  "type": "module"
}

This allows you to use syntax like import express from 'express', which aligns well with frontend JavaScript code.

📦 Handling JSON Data in Backend
Handling JSON data in Express.js is very easy with the express.json() middleware, which automatically parses incoming JSON requests:

jsCopyEditimport express from 'express';
const app = express();

app.use(express.json());// this is middle ware 

app.post('/api/data', (req, res) => {
  console.log(req.body);  // Parsed JSON data available here
  res.send('Data received');
});
// by vivek verma !!

⚙️ Simple APIs & Axios Advantages
Creating simple APIs and calling them using Axios provides several benefits:

  • Automatic JSON serialization and parsing (no manual stringify/parse needed)

  • Support for request cancellation

  • Progress tracking for file uploads/downloads

🔒 Understanding CORS & Whitelisting
CORS (Cross-Origin Resource Sharing) is a browser security feature that allows only trusted domains to access backend APIs. Whitelisting restricts API access to specific origins:

jsCopyEditimport cors from 'cors';

const whitelist = ['http://localhost:3000', 'https://myapp.com'];

const corsOptions = {
  origin: function(origin, callback) {
    if (whitelist.indexOf(origin) !== -1 || !origin) {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  }
};

app.use(cors(corsOptions));
// by vivek verma !!

🔀 Proxy Server Usage with React & Vite
When frontend (React/Vite) and backend run on different ports, CORS issues occur. Setting up a proxy is the best solution:

  • In React, add "proxy": "http://localhost:5000" to package.json.

  • In Vite, configure proxy in vite.config.js.

🚫 Important Bad Practice Reminder
Avoid serving frontend dist folder directly from backend using express.static("dist"), because:

  • It makes frontend updates difficult

  • It reduces code modularity and maintainability

  • In production, it’s better to deploy frontend and backend separately


Conclusion
Following these modern backend development techniques and best practices helps build more efficient and secure applications:

  • Use ESM syntax

  • Handle JSON with Express middleware

  • Leverage Axios advantages

  • Implement CORS policy with whitelisting

  • Use proxy setup during development

  • Avoid serving frontend dist directly from backend

These practices contribute to cleaner, safer, and more maintainable backend code.

1
Subscribe to my newsletter

Read articles from Vivek Kumar Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vivek Kumar Verma
Vivek Kumar Verma