DEV Community

Cover image for Bread n’ Butter: Node.js, Express and Apollo
Luis Faria
Luis Faria

Posted on

Bread n’ Butter: Node.js, Express and Apollo

Moving on with the BNB: Bread n’ Butter 🍞🧈 series, it’s time to look at the other half of the MERN stack: Node.js, Express, and Apollo.

This combo is fast, flexible, and free to use - powering everything from REST APIs to GraphQL endpoints.


Backend (Node.js/Express/Apollo)

Daily tools when wiring APIs


1. Modules (import/require)

  • Split code into small files: db.ts, user.ts, article.ts
  • Node's bread and butter: everything is modular!
  • Example:
import { db } from './db';
import { user } from './user';
import { article } from './article';
Enter fullscreen mode Exit fullscreen mode

Modules


2. Asyncronous operations (async/await)

  • Almost everything on the backend is async: database queries, API calls, file system ops. That’s why async/await is your best friend.
  • Example:
const user = await db.user.findById(id);
Enter fullscreen mode Exit fullscreen mode

Asynchronous Ops


3. Routing/Resolvers

  • Express handles REST-style routes:
app.get('/user/:id', (req, res) => { ... })
Enter fullscreen mode Exit fullscreen mode
  • Apollo handles GraphQL queries/mutations:
userQueries.user = async (_: any, { id }: { id: string }) => { ... }
Enter fullscreen mode Exit fullscreen mode
  • Just like Next.js has pages, these wire up your API logic.

Routing
Resolvers


4. Middleware

  • Functions that sit between requests and responses. Think of them as "filters" or "guards".
  • Express:
app.use((req, res, next) => { ... })
Enter fullscreen mode Exit fullscreen mode
  • Apollo:
expressMiddleware(server, { 
  context: async ({ req }: any) => { ... } 
})
Enter fullscreen mode Exit fullscreen mode
  • Examples: Authentication, Logging, Rate Limiting

Middleware


5. Environment Config (dotenv)

  • Never hardcode secrets. Use environment variables for things like PORT, DB_URI, API keys.
  • Example:
const port = process.env.PORT || 3000;
const dbUri = process.env.DB_URI || 'mongodb://localhost:27017/myapp';
Enter fullscreen mode Exit fullscreen mode

Environment


6. Database Models (Mongoose)

  • This is the backend’s “state” — persistent data.
  • Define structure and relationships using schemas.
  • Example:
const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  password: String,
});
Enter fullscreen mode Exit fullscreen mode

Database Models


The Bread n’ Butter Flow:

Modules → Async/Await → Routing/Resolvers → Middleware → Env Config → Database Models


Master these, and you can wire up APIs for anything from a small SaaS to enterprise-scale systems.


I move forward in the next post with BNB: MongoDB & React Integration

How about you, this time, any personal “bread n’ butter” in backend?
Anything you can’t live without? Share with me, I'm even more curious!!!

Top comments (0)