Express application generator
Use the application generator tool, express-generator, to quickly create an application skeleton.
You can run the application generator with the npx command (available in Node.js 8.2.0).
$npxexpress-generatorFor earlier Node versions, install the application generator as a global npm package and then launch it:
$npminstall-gexpress-generator$expressDisplay the command options with the -h option:
$express-hUsage:express [options] [dir]Options:-h,--helpoutputusageinformation--versionoutputtheversionnumber-e,--ejsaddejsenginesupport--hbsaddhandlebarsenginesupport--pugaddpugenginesupport-H,--hoganaddhogan.jsenginesupport--no-viewgeneratewithoutviewengine-v,--view<engine>addview<engine>support (ejs|hbs|hjs|jade|pug|twig|vash) (defaultstojade)-c,--css<engine>addstylesheet<engine>support (less|stylus|compass|sass) (defaultstoplaincss)--gitadd.gitignore-f,--forceforceonnon-emptydirectoryFor example, the following creates an Express app named myapp. The app will be created in a folder named myapp in the current working directory and the view engine will be set to Pug:
$express--view=pugmyappcreate:myappcreate:myapp/package.jsoncreate:myapp/app.jscreate:myapp/publiccreate:myapp/public/javascriptscreate:myapp/public/imagescreate:myapp/routescreate:myapp/routes/index.jscreate:myapp/routes/users.jscreate:myapp/public/stylesheetscreate:myapp/public/stylesheets/style.csscreate:myapp/viewscreate:myapp/views/index.pugcreate:myapp/views/layout.pugcreate:myapp/views/error.pugcreate:myapp/bincreate:myapp/bin/wwwThen install dependencies:
$cdmyapp$npminstallOn MacOS or Linux, run the app with this command:
$DEBUG=myapp:*npmstartOn Windows Command Prompt, use this command:
> set DEBUG=myapp:* & npmstartOn Windows PowerShell, use this command:
PS> $env:DEBUG='myapp:*'; npmstartThen, 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.pug7directories,9filesNote
The app structure created by the generator is just one of many ways to structure Express apps. Feel free to use this structure or modify it to best suit your needs.
