MDN wants to learn about developers like you:

Learn web development

Учебник Express часть 2: Создание скелета сайта

Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.

Эта вторая статья в нашем учебнике Express показывает, как вы можете создать «скелет» проекта веб-сайта, который затем можно будет заполнить с помощью маршрутов сайта, шаблонов/представлений и вызовов базы данных.

Необходимые знания: Установить среду разработки Node. Просмотреть обзорно учебник Express.
Задача: Научиться запускать свои проекты используя Express Application Generator.


В этой статье показано как вы можете создать "скелет" сайта с помощью инструмента Express Application Generator, который затем можно будет заполнить с помощью маршрутов сайта, шаблонов/представлений и вызовов базы данных. В этом случае мы будем использовать инструмент для создания основы для нашего сайта Local Library, к которому мы добавим весь другой код необходимый сайту. Процесс чрезвычайно прост, требуяся только, чтобы вы вызвали генератор в командной строке с новым именем проекта, опционально также определяя механизм шаблона сайта и генератор CSS.

В следующих разделах показано, как вызвать генератор приложений, и дает небольшое объяснение различных вариантов просмотра/CSS. Мы также объясним, как структурирован скелет веб-сайта. В конце мы покажем, как вы можете запустить веб-сайт, чтобы убедиться, что он работает.

Замечание: Express Application Generator генератор не только для Express приложений, и генерирование проекта не единтсвенный способ организации для ваших файлов и директорий. Однако созданный сайт имеет модульную структуру, которую легко расширить и понять. Для информации о минимальном Express приложении, смотрите Hello world example (документация Express).

Использование генератора приложений

Вы уже должны были устанавливать генератор в статье установка среды разработки Node. Для быстрого напоминания, вы установили генератор используя пакетный менеджер NPM, следующим образом:

npm install express-generator -g

Генератор имеет несколько параметров, которые вы можете просмотреть в командной строке с помощью команды --help (или -h):

> express --help

  Usage: express [options] [dir]


    -h, --help           output usage information
        --version        output the version number
    -e, --ejs            add ejs engine support
        --pug            add pug engine support
        --hbs            add handlebars engine support
    -H, --hogan          add hogan.js engine support
    -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

Вы можете просто указать express для создания проекта в текущем каталоге с использованием механизма просмотра Jade и обычного CSS (если вы укажете имя каталога, тогда проект будет создан в подпапке с этим именем).


Вы также можете выбрать механизм просмотра (шаблона), используя --view и/или механизм генерации CSS с помощью --css.

Заметка: Другие опции для выбора шаблонизатора (такие как --hogan, --ejs, --hbs и пр.) утсарели. Используйте --view (или -v)!

What view engine should I use?

The Express Application Generator allows you to configure a number of popular view/templating engines, including EJS, Hbs, Pug (Jade), Twig, and Vash, although it chooses Jade by default if you don't specify a view option. Express itself can also support a large number of other templating languages out of the box.

Note: If you want to use a template engine that isn't supported by the generator then see Using template engines with Express (Express docs) and the documentation for your target view engine.

Generally speaking you should select a templating engine that delivers all the functionality you need and allows you to be productive sooner — or in other words, in the same way that you choose any other component! Some of the things to consider when comparing template engines:

  • Time to productivity — If your team already has experience with a templating language then it is likely they will be productive faster using that language. If not, then you should consider the relative learning curve for candidate templating engines.
  • Popularity and activity — Review the populartity of the engine and whether it has an active community. It is important to be able to get support for the engine when you have problems over the lifetime of the website.
  • Style — Some template engines use specific markup to indicate inserted content within "ordinary" HTML, while others construct the HTML using a different syntax (for example, using indentation and block names).
  • Performance/rendering time.
  • Features — you should consider whether the engines you look at have the following features available:
    • Layout inheritence: Allows you to define a base template and then "inherit" just the parts of it that you want to be different for a particular page. This is typically a better approach than building templates by including a number of required components, or building a template from scratch each time.
    • "Include" support: Allows you to build up templates by including other templates.
    • Concise variable and loop control syntax.
    • Ability to filter variable values at template level (e.g. making variables upper-case, or formatting a date value).
    • Ability to generate output formats other than HTML (e.g. JSON or XML).
    • Support for asynchronous operations and streaming.
    • Can be used on the client as well as the server. If a templating engine can be used on the client this allows the possibility of serving data and having all or most of the rendering done client-side.

Tip: There are many resources on the Internet to help you compare the different options!

For this project we'll use the Pug templating engine (this is the recently-renamed Jade engine), as this is one of the most popular Express/JavaScript templating languages and is supported out of the box by the generator.

What CSS stylesheet engine should I use?

The Express Application Generator allows you to create a project that is configured to use the most common CSS stylesheet engines: LESS, SASS, Compass, Stylus.

Note: CSS has some limitations that make certain tasks difficult. CSS stylesheet engines allow you to use more powerful syntax for defining your CSS, and then compile the definition into plain-old CSS for browsers to use.

As with templating engines, you should use the stylesheet engine that will allow your team to be most productive. For this project we'll use the ordinary CSS (the default) as our CSS requirements are not sufficiently complicated to justify using anything else.

What database should I use?

The generated code doesn't use/include any databases. Express apps can use any database mechanism supported by Node (Express itself doesn't define any specific additional behaviour/requirements for database management).

We'll discuss how to integrate with a database in a later article.

Creating the project

For the sample Local Library app we're going to build, we'll create a project named express-locallibrary-tutorial using the Pug template library and no CSS stylesheet engine.

First navigate to where you want to create the project and then run the Express Application Generator in the command prompt as shown:

express express-locallibrary-tutorial --view=pug

The generator will create (and list) the project's files.

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

   install dependencies:
     > cd express-locallibrary-tutorial && npm install

   run the app:
     > SET DEBUG=express-locallibrary-tutorial:* & npm start

At the end of the output the generator provides instructions on how you install the dependencies (as listed in the package.json file) and then how to run the application (the instructions above are for Windows; on Linux/Mac OS X they will be slightly different).

Running the skeleton website

At this point we have a complete skeleton project. The website doesn't actually do very much yet, but it's worth running it to show how it works.

  1. First install the dependencies (the install command will fetch all the dependency packages listed in the project's package.json file).
    cd express-locallibrary-tutorial
    npm install
  2. Then run the application.
    • On Windows, use this command:
      SET DEBUG=express-locallibrary-tutorial:* & npm start
    • On Mac OS X or Linux, use this command:
      DEBUG=express-locallibrary-tutorial:* npm start
  3. Then load http://localhost:3000/ in your browser to access the app.

You should see a browser page that looks like this:

Browser for default Express app generator website

You have a working Express application, serving itself to localhost:3000.

Note: You could also start the app just using the npm start command. Specifying the DEBUG variable as shown enables console logging/debugging. For example, when you visit the above page you'll see debug output like this:

>SET DEBUG=express-locallibrary-tutorial:* & npm start

> express-locallibrary-tutorial@0.0.0 start D:\express-locallibrary-tutorial
> node ./bin/www

  express-locallibrary-tutorial:server Listening on port 3000 +0ms
GET / 200 288.474 ms - 170
GET /stylesheets/style.css 200 5.799 ms - 111
GET /favicon.ico 404 34.134 ms - 1335

Enable server restart on file changes

Any changes you make to your Express website are currently not visible until you restart the server. It quickly becomes very irritating to have to stop and restart your server every time you make a change, so it is worth taking the time to automate restarting the server when needed.

One of the easiest such tools for this purpose is nodemon. This is usually installed globally (as it is a "tool"), but here we'll install and use it locally as a developer dependency, so that any developers working with the project get it automatically when they install the application. Use the following command in the root directory for the skeleton project:

npm install --save-dev nodemon

If you open your project's package.json file you'll now see a new section with this dependency:

  "devDependencies": {
    "nodemon": "^1.11.0"

Because the tool isn't installed globally we can't launch it from the command line (unless we add it to the path) but we can call it from an NPM script because NPM knows all about the installed packages. Find the the scripts section of your package.json. Initially it will contain one line, which begins with "start". Update it by putting a comma at the end of that line, and adding the "devstart" line seen below:

  "scripts": {
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"

We can now start the server in almost exactly the same way as previously, but with the devstart command specified:

SET DEBUG=express-locallibrary-tutorial:* & npm run devstart

Now if you edit any file in the project the server will restart (or you can restart it by typing rs on the command prompt at any time). You will still need to reload the browser to refresh the page.

Note: We now have to call "npm run <scriptname>" rather than just npm start, because "start" is actually an NPM command that is mapped to the named script. We could have replaced the command in the start script but we only want to use nodemon during development, so it makes sense to create a new script command.

The generated project

Let's now take a look at the project we just created.

Directory structure

The generated project, now that you have installed dependencies, has the following file structure (files are the items not prefixed with "/"). The package.json file defines the application dependencies and other information. It also defines a startup script that will call the application entry point, the JavaScript file /bin/www. This sets up some of the application error handling and then loads app.js to do the rest of the work. The app routes are stored in separate modules under the routes/ directory. The templates are stored under the /views directory.

        [about 4,500 subdirectories and files]

The following sections describe the files in a little more detail.


The package.json file defines the application dependencies and other information:

  "name": "express-locallibrary-tutorial",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"
  "dependencies": {
    "body-parser": "~1.15.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.14.0",
    "morgan": "~1.7.0",
    "pug": "~2.0.0-beta6",
    "serve-favicon": "~2.3.0"
  "devDependencies": {
    "nodemon": "^1.11.0"

The dependencies include the express package and the package for our selected view engine (pug). In addition, we have the following packages that are useful in many web applications:

  • body-parser: This parses the body portion of an incoming HTTP request and makes it easier to extract different parts of the contained information. For example, you can use this to read POST parameters.
  • cookie-parser: Used to parse the cookie header and populate req.cookies (essentially provides a convenient method for accessing cookie information).
  • debug: A tiny node debugging utility modelled after node core's debugging technique.
  • morgan: An HTTP request logger middleware for node.
  • serve-favicon: Node middleware for serving a favicon (this is the icon used to represent the site inside the browser tab, bookmarks, etc.).

The scripts section defines a "start" script, which is what we are invoking when we call npm start to start the server. From the script definition you can see that this actually starts the JavaScript file ./bin/www with node. It also defines a "devstart" script, which we invoke when calling npm run devstart instead. This starts the same ./bin/www file, but with nodemon rather than node.

  "scripts": {
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"

www file

The file /bin/www is the application entry point! The very first thing this does is require() the "real" application entry point (app.js, in the project root) that sets up and returns the express() application object.

#!/usr/bin/env node

 * Module dependencies.

var app = require('../app');

Note: require() is a global node function that is used to import modules into the current file. Here we specify app.js module using a relative path and omitting the optional (.js) file extension.

The remainder of the code in this file sets up a node HTTP server with app set to a specific port (defined in an environment variable or 3000 if the variable isn't defined), and starts listening and reporting server errors and connections. For now you don't really need to know anything else about the code (everything in this file is "boilerplate"), but feel free to review it if you're interested.


This file creates an express application object (named app, by convention), sets up the application with various settings and middleware, and then exports the app from the module. The code below shows just the parts of the file that create and export the app object:

var express = require('express');
var app = express();
module.exports = app;

Back in the www entry point file above, it is this module.exports object that is supplied to the caller when this file is imported.

Lets work through the app.js file in detail. First we import some useful node libraries into the file using require(), including express, serve-favicon, morgan, cookie-parser and body-parser that we previously downloaded for our application using NPM; and path, which is a core Node library for parsing file and directory paths.

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

Then we require() modules from our routes directory. These modules/files contain code for handling particular sets of related "routes" (URL paths). When we extend the skeleton application, for example to list all books in the library, we will add a new file for dealing with book-related routes.

var index = require('./routes/index');
var users = require('./routes/users');

Note: At this point we have just imported the module; we haven't actually used its routes yet (this happens just a little bit further down the file).

Next we create the app object using our imported express module, and then use it to set up the view (template) engine. There are two parts to setting up the engine. First we set the 'views' value to specify the folder where the templates will be stored (in this case the sub folder /views). Then we set the 'view engine' value to specify the template library (in this case "pug").

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

The next set of functions call app.use() to add the middleware libraries into the request handling chain. In addition to the 3rd party libraries we imported previously, we use the Express.static middleware to get Express to serve all the static files in the directory /public in the project root.

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

Now that all the other middleware is set up, we add our (previously imported) route-handling code to the request handling chain. The imported code will define particular routes for the different parts of the site:

app.use('/', index);
app.use('/users', users);

Note: The paths specified above ('/' and '/users') are treated as a prefix to routes defined in the imported files. So for example if the imported users module defines a route for /profile, you would access that route at /users/profile. We'll talk more about routes in a later article.

The last middleware in the file adds handler methods for errors and HTTP 404 responses.

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error ='env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);

The Express application object (app) is now fully configured. The last step is to add it to the module exports (this is what allows it to be imported by /bin/www).

module.exports = app;


The route file /routes/users.js is shown below (route files share a similar structure, so we don't need to also show index.js). First it loads the express module, and uses it to get an express.Router object. Then it specifies a route on that object, and lastly exports the router from the module (this is what allows the file to be imported into app.js).

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

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');

module.exports = router;

The route defines a callback that will be invoked whenever an HTTP GET request with the correct pattern is detected. The matching pattern is the route specified when the module is imported ('/users') plus whatever is defined in this file ('/'). In other words, this route will be used when an URL of /users/ is received.

Tip: Try this out by running the server with node and visiting the URL in your browser: http://localhost:3000/users/. You should see a message: 'respond with a resource'.

One thing of interest above is that the callback function has the third argument 'next', and is hence a middleware function rather than a simple route callback. While the code doesn't currently use the next argument, it may be useful in the future if you add want to add multiple route handlers to the '/' route path.

Views (templates)

The views (templates) are stored in the /views directory (as specified in app.js) and are given the file extension .pug. The method Response.render() is used to render a specified template along with the values of named variables passed in an object, and then send the result as a response. In the code below from /routes/index.js you can see how that route renders a response using the template "index" passing the template variable "title".

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });

The corresponding template for the above route is given below (index.pug). We'll talk more about the syntax later. All you need to know for now is that the title variable (with value 'Express') is inserted where specified in the template.

extends layout

block content
  h1= title
  p Welcome to #{title}

Challenge yourself

Create a new route in /routes/users.js that will display the text "You're so cool" at URL /users/cool/. Test it by running the server and visiting http://localhost:3000/users/cool/ in your browser


You have now created a skeleton website project for the Local Library and verified that it runs using node. Most important, you also understand how the project is structured, so you have a good idea where we need to make changes to add routes and views for our local library.

Next we'll start modifying the skeleton so that works as a library website.

See also

Метки документа и участники

 Внесли вклад в эту страницу: daviys
 Обновлялась последний раз: daviys,