Scaffolding an app with Express Generator

The Node.js ecosystem

Earlier we built a small Express server by hand, and that then grew that into a real app. That's the right way to learn - you know what every line is for.

Sometimes, though, you just want a skeleton: folders for routes, views, static files, and a package.json with the right bits already wired up. That's what Express Generator does. It's a command-line tool that writes a starter Express application for you in a few seconds.

An honest note before we start

The package is called express-generator. You run it with npx, which comes with npm (and therefore with nodejs24). You do not need to install it globally:

bash

npx express-generator --help

A few facts worth knowing, because the internet is full of older instructions:

  • The generator scaffolds an Express 4 app. As of writing, there is no Express 5 version of express-generator, and the Express team has talked about retiring or replacing it. It still works, and the official Express 4 (and even 5) docs still describe it, but it is not "the newest thing."
  • Older blog posts (including some excellent IBM i sessions) say npm install express-generator --global and then express -e demoApp. That still works, but npx is cleaner: no global package to keep patched, and no PATH surprises when you switch Node versions with nodever.
  • The generator does not install Express for you as a finished dependency. It writes a package.json. You then run npm install inside the new folder, and that is what pulls Express and friends into node_modules.

If you want a modern Express 5 app, the course way is still the best one: npm init -y, npm install express, and write it yourself the way we did in Section 4. Treat the generator as a shortcut for a classic Express 4 site, not as a requirement.

Generate a skeleton

Pick a folder in the IFS - I'll use your home directory - and ask the generator for EJS views (-e or --view=ejs). EJS is plain HTML with a few <%= %> markers, which is a gentle start if you've never used a template engine.

bash

cd ~
npx express-generator --view=ejs demoApp
cd demoApp
npm install

You'll see it create a handful of files and folders. Then npm install reads the generated package.json and fetches the dependencies (Express, EJS, and a few middleware packages such as morgan for request logging and cookie-parser).

Have a look at that package.json. The generator still pins Express 4.16-era versions (for example express: ~4.16.1). That's fine for a skeleton, and it will run. If you're starting a real project from it, bump those ranges to current Express 4 releases (or move to Express 5 and adjust the code) before you go much further.

What's in the box

Inside an Express Generator App

That's one of many possible layouts, not a law of physics. The idea is:

  • app.js sets up middleware (including express.json() for JSON bodies and express.urlencoded for form posts) and mounts the routers from routes.
  • bin/www loads app.js and calls listen. The port is process.env.PORT or 3000
  • routes holds one file per area of the site. This is the same routing idea we discussed earlier, just split across files
  • views/ holds EJS templates. res.render('index', { title: 'Express' }) fills them in
  • public/ is static files - CSS, browser JavaScript, images - served automatically

Have a poke around. `routes/index.js` is only a few lines:

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});

module.exports = router;

Yes, you are right! The generator still writes var. You can change it to const if it offends you. It will not offend the code 🙂

Run it on IBM i

bash

npm start

Or, to see the debug output the generator wired up (this is bash, so it works in our SSH session). Note the lowercase demoapp - the generator lowercases the folder name when it sets up the debug namespace:

bash

DEBUG=demoapp:* npm start

Then, from your PC's browser: http://myibmi:3000/

You should get a page that says "Express" and "Welcome to Express." The /users route is a plain-text stub. Ctrl+C in the terminal stops the server, same as always.

Port already in use? If the ingredients app from Section 5 is still running on 3000 (perhaps under PM2), either stop it (pm2 stop ingredients-app) or start this one on another port: PORT=3001 npm start

Where you'd take it next

This skeleton is a starting point, not a finished application. To make it talk to IBM i you'd do the same things we already did by hand:

  • npm install odbc dotenv and drop in a db.js
  • add routes that call db.query(...) and return JSON, or res.render(...) with the rows
  • call RPG with itoolkit where the business logic already exists

For an API-only service, you may not want views at all:

bash

npx express-generator --no-view demoApi

That skips the template engine and gives you a slimmer skeleton.

So: the generator is a fast way to get a traditional Express folder tree. You already know how to do the interesting part - the routes, the SQL, the RPG calls - without it. Use whichever starting point makes you faster.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>