Skip to content
tezvyn:

Scaffold an Express App with `express-generator`

Source: expressjs.comMediumHow cards are made

Scaffold an Express App with `express-generator`

The express-generator CLI is a blueprint for new Express apps, instantly creating a standard folder structure and boilerplate files. Use it to skip tedious setup of routes and views.

Why it exists

Starting a new web application involves creating a lot of boilerplate: directories for routes, views, and static files, plus a basic server setup. The express-generator tool automates this repetitive process, letting you focus on building features instead of project setup.

The mental model

express-generator is a scaffolding tool. Like construction scaffolding, it provides a standard, temporary structure to get your project off the ground quickly. It generates a complete, runnable Express app with a conventional file layout that you can then modify and build upon.

How it works

You run the tool from your terminal using npx express-generator [options] [app-name]. It creates a new directory with the app name and populates it with subdirectories like public, routes, and views. It also generates key files like app.js (the main app configuration), package.json (project dependencies), and bin/www (the script that starts the server). After generation, you navigate into the new directory, run npm install to get the dependencies, and then npm start to run the server.

When to use it

Use express-generator when you're starting a new, traditional server-side rendered Express application. It's especially helpful for beginners learning the standard structure of an Express app or for quickly prototyping ideas where you need a full file structure immediately.

When not to use it

Don't use it if you're building a minimal API server that doesn't require a view engine or a complex file structure. In such cases, creating a single index.js file manually is often faster and results in less code to delete. Also, avoid relying on its default configuration for production without a thorough review and customization for security and error handling.

One canonical example

To create a new app called my-app using the Pug template engine and adding a .gitignore file, you would run: npx express-generator --view=pug --git my-app. The tool will then output the list of created files. The next steps are to cd my-app, run npm install, and finally npm start to see the default welcome page.

Interview question

Which scenario would make express-generator the LEAST suitable tool for initiating an Express project?

  • a.Developing a new web application that uses server-side rendering.
  • b.Rapidly prototyping an application requiring a complete file layout.
  • c.Creating a simple API server that primarily returns JSON responses.Correct
  • d.Setting up a project to learn the conventional Express file structure.
Why?

The card explicitly states not to use express-generator for 'a minimal API server that doesn't require a view engine or a complex file structure,' which aligns with creating a simple API returning JSON. The other options describe scenarios where the tool is highly recommended.

Just read this? Test yourself on what you have been reading.

Read the original → expressjs.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles