Tento dokument môže byť v porovnaní s dokumentáciou v angličtine zastaralý. Aktuálne informácie nájdete v dokumentácii v angličtine.

âś–

Express generátor

Pre rýchle vygenerovanie skeletonu aplikácie môžete použit nástroj express-generator.

Nainštalujte express-generator pomocou nasledujúceho príkazu:

$ npm install express-generator -g

Pre zobrazenie ďalších možností príkazu zadajte prepínač -h:

$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information
        --version       output the version number
    -e, --ejs           add ejs engine support
        --hbs           add handlebars engine support
        --pug           add pug engine support
    -H, --hogan         add hogan.js engine support
        --no-view       generate without view engine
    -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
    -c, --css <engine>  add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
        --git           add .gitignore
    -f, --force         force on non-empty directory

Nasledujúci príkaz vytvorí v aktuálnom priečinku Express aplikáciu s názvom myapp:

$ express --view=pug myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css
   create : myapp/views
   create : myapp/views/index.pug
   create : myapp/views/layout.pug
   create : myapp/views/error.pug
   create : myapp/bin
   create : myapp/bin/www

Potom nainštalujte dependencie:

$ cd myapp
$ npm install

Na MacOS príp. Linux, spustíte aplikáciu príkazom:

$ DEBUG=myapp:* npm start

Na Windows, prĂ­kazom:

> set DEBUG=myapp:* & npm start

Potom v prehliadaÄŤi zadajte http://localhost:3000/.

Vygenerovaná aplikácia má naslednovnú štruktúru priečinkov:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug

7 directories, 9 files

Takáto štruktúra aplikácie je len jedným z mnohých spôsobov usporiadania Express aplikácie. Môžete ju použit, alebo ju zmeniť tak, ako vám bude najlepšie vyhovovať.