The Express application generator is a powerful tool that allows developers to quickly create a skeleton for their Express.js applications, streamlining setup and configuration. This guide will walk you through the process of using the Express application generator to create a new Express app.
To use the Express application generator, you can run it with the npx
command (available in Node.js 8.2.0) or install it as a global npm package. To install it globally, run the following command:
npm install -g express-generator
Then, launch the generator with the following command:
express
You can display the command options with the -h
option:
express -h
This will show you the available options, including the ability to specify a view engine, stylesheet engine, and more.
To create a new Express app, run the following command:
express --view=pug myapp
This will create a new app named myapp
in a folder named myapp
in the current working directory, with Pug as the view engine. You can then install dependencies with the following command:
cd myapp
npm install
To run the app, use the following command on MacOS or Linux:
DEBUG=myapp:* npm start
On Windows Command Prompt, use this command:
set DEBUG=myapp:* & npm start
On Windows PowerShell, use this command:
$env:DEBUG='myapp:*'; npm start
Then, load http://localhost:3000/
in your browser to access the app.
The generated app has the following directory structure:
.
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.pug
├── index.pug
└── layout.pug
This structure is just one of many ways to structure Express apps, and you can modify it to suit your needs.
For more information on using the Express application generator, check out the official Express documentation. You can also explore the Express GitHub repository for more resources and examples.
Some other topics you may want to explore include:
By using the Express application generator, you can quickly and easily create a new Express app and start building your next project.