这篇翻译不完整。请帮忙从英语翻译这篇文章

在这篇第一篇文章中,我们回答了“什么是Node?”以及“什么是Express?”,让您大致了解一下什么使Express web框架变得特别。我们将概述主要特性,并向您展示一个Express应用程序的主要构建块(尽管目前还没有一个用于测试的开发环境)。

先决条件: 基本的计算机知识。对服务器端网站编程的一般理解,特别是在网站上的客户机-服务器交互的机制
Objective: 要熟悉Express是什么,以及它是如何与节点相适应的,它提供了什么功能,以及Express应用程序的主要构建块。

Express and Node 是什么?

Node(或更正式的Node.js)是一个开源的、跨平台的、运行时环境,允许开发人员在JavaScript中创建各种服务器端工具和应用程序。运行时的目的是在浏览器环境之外使用(即直接在计算机或服务器操作系统上运行)。因此,环境会省略特定于浏览器的JavaScript api,并支持更多传统的操作系统api,包括HTTP和文件系统库。

从web服务器开发的角度来看,Node有很多好处:

  • 伟大的表现!Node的设计目的是优化web应用程序的吞吐量和可伸缩性,并且非常适合许多常见的web开发问题(例如,实时web应用程序)。
  • 代码是用“普通的旧JavaScript”编写的,这意味着在编写浏览器和web服务器代码时,处理语言之间的“上下文转换”的时间更少。
  • JavaScript是一种相对较新的编程语言,与其他传统的web服务器语言(例如Python、PHP等)相比,语言设计的改进带来了好处。许多其他的新流行的语言编译/转换成JavaScript,所以你也可以使用CoffeeScript, ClosureScript、LiveScript、生活脚本等等。
  • node包管理器(NPM)提供了对数十万个可重用包的访问。它还具有最佳的类依赖性解决方案,并且可以用于自动化大多数构建工具链.
  • 它是可移植的,版本运行在Microsoft Windows、OS X、Linux、Solaris、FreeBSD、OpenBSD、WebOS和不间断操作系统上。此外,它还受到许多web主机提供商的支持,它们通常为托管节点站点提供特定的基础设施和文档。
  • 它有一个非常活跃的第三方生态系统和开发者社区,有很多人愿意提供帮助。

     

您可以简单地创建一个简单的web服务器,以使用hodeHTTP包来响应任何请求,如下所示。这将创建一个服务器,并在URL http://127.0.0.1:8000/上侦听任何HTTP请求;当一个接收到的时候,它将发送一个纯文本响应“Hello World”。

// Load HTTP module
var http = require("http");

// Create HTTP server and listen on port 8000 for requests
http.createServer(function(request, response) {

   // Set the response HTTP header with HTTP status and Content type
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body "Hello World"
   response.end('Hello World\n');
}).listen(8000);

// Print URL for accessing server
console.log('Server running at http://127.0.0.1:8000/');

其他常见的web开发任务不受node本身的直接支持。如果你想添加特定的处理不同的HTTP动词(如GET、POST、DELETE等),分别在不同的URL路径处理请求(“路线”),提供静态文件,或使用模板来动态创建响应,那么您将需要编写自己的代码,或者你也可以避免重新发明轮子和使用web框架!

Express是最流行的node web框架,它是许多其他流行的节点web框架的底层库。它提供了机制:

  • 在不同的URL路径(路由)中使用不同HTTP动词的请求编写处理程序。
  • 与“视图”呈现引擎集成,以便通过将数据插入模板来生成响应。
  • 设置常见的web应用程序设置,比如用于连接的端口,以及用于呈现响应的模板的位置。
  • 在请求处理管道的任何位置添加额外的请求处理“中间件”。

虽然Express本身是非常简单的,但是开发人员已经创建了兼容的中间件包来解决几乎所有的web开发问题。有一些库可以使用cookie、会话、用户登录、URL参数、POST数据、安全标头等等。您可以在Express中间件中找到由Express团队维护的中间件软件包列表(以及一些流行的第三方软件包列表)。

Note: 这种灵活性是一把双刃剑。有一些中间件包可以解决几乎所有的问题或需求,但是使用合适的包有时是一种挑战。在构建应用程序的过程中也没有“正确的方法”,而且您在Internet上可能发现的许多示例不是最优的,或者只是为了开发web应用程序,只显示您需要做的一小部分。

它从哪儿来的?

Node最初是在2009年发布的,只针对Linux。NPM包管理器于2010年发布,并在2012年添加了本地窗口支持。当前的LTS版本是Nodev6.11.11,而最新版本是Node8。这是一个丰富历史的小快照;如果你想知道更多的话,可以深入维基百科。

Express最初于2010年11月发布,目前在API的版本4中。您可以通过签出变更来了解当前版本中的变更信息,以及GitHub提供更详细的历史发行记录。

Node/Express 有多流行?

web框架的流行非常重要,因为它是一个指示器,说明它是否会继续维护,以及在文档、附加库和技术支持方面可能有哪些资源可用。

对于服务器端框架的流行程度,目前还没有任何准备和明确的度量(尽管像热框架这样的网站试图通过计算每个平台的GitHub项目数量和StackOverflow问题等机制来评估受欢迎程度)。一个更好的问题是,Node和Express是否“足够流行”,以避免不受欢迎的平台的问题。它们还在继续进化吗?如果你需要帮助,你能得到帮助吗?如果你学习Express,你有机会获得报酬吗?

基于使用Express的高知名度公司的数量,为代码库做出贡献的人数,以及提供免费和付费支持的人员数量,是的,Express是一个流行的框架!

Is Express opinionated?

Web框架通常将自己称为“固执己见”或“不固执己见”。

固执己见的框架是那些对处理任何特定任务的“正确方法”有意见的人。它们通常支持特定领域的快速开发(解决特定类型的问题),因为正确的方法通常是很好的理解和记录的。然而,在解决主要领域之外的问题时,它们可能不那么灵活,而且对于可以使用的组件和方法,它们提供的选择也更少。

相比之下,那些不固执己见的框架,对于将组件粘合在一起以实现目标的最佳方式,甚至应该使用什么组件的限制,都要少得多。它们使开发人员更容易使用最合适的工具来完成特定的任务,尽管成本是您自己需要找到这些组件的成本。


Express是不固执己见的。您几乎可以将任何您喜欢的任何兼容的中间件插入到请求处理链中。您可以在一个文件或多个文件中构造该应用程序,并使用任何目录结构。有时候你会觉得自己有太多的选择!

Express代码看起来的样子?

 

在传统的数据驱动网站,一个web应用是等待来自浏览器或其它客户端的HTTP请求的。当web应用收到一个请求,它会根据URL中包含的参数信息,以及POST方式发过来的数据,来解析请求需要的功能。这个取决于根据请求从服务器数据库读取到的信息。web应用返回给浏览器一个由HTML模板的占位符上插入数据动态生成的HTML页面。

Express提供了一些方法来指定特定的HTTP动作(GET, POST, SET等)和URL模式(“路由”)所调用的函数,以及指定使用何种模板(“view”)引擎、模板文件所在位置以及使用哪个模板用来渲染响应(返回内容)的方法。您可以使用Express中间件来添加对cookie、会话和用户,获得POST/GET参数等等的支持。您可以使用Node支持的任何数据库机制(Express没有定义任何与数据库相关的行为)。

下面的部分将解释在书写Express和Node代码时您将遇到的一些常见问题。

Helloworld Express

首先让我们思考一下标准的 ExpressHello World的示例(我们将在下面讨论它的各个部分)。

点子: 如果您已经安装了NodeExpress(或者您按照下一篇文章中的说明去安装它们),可以将此代码保存在名为app.js的文件中,并通过在bash命令提示符中调用以下命令运行它

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.listen(3000, function() {
  console.log('Example app listening on port 3000!');
});

前两行(导入)express模块,并创建 Express 应用程序。这个对象,传统上被命名为 app,具有路由HTTP请求、配置中间件、渲染HTML视图、注册模板引擎以及修改应用程序设置的方法,这些设置控制应用程序的行为(例如,环境模式,路由定义是否为区分大小写等)。

代码的中间部分(从app.get()开始的三行)显示了路由定义。app.get()方法指定了一个回调函数,每当有一个HTTP GET请求具有一个相对于站点根目录的路径('/')时,该回调函数都会被调用。回调函数接受一个请求和一个响应对象作为参数,并简单地调用响应中的send()来返回字符串“Hello World!”

最后一个代码块在端口“3000”上启动服务器,并将日志注释打印到控制台。在服务器运行时,您可以在浏览器中访问localhost:3000,以查看返回的响应示例。

导入和创建模块

模块是一个JavaScript库/文件,您可以使用Node的 require() 函数将其导入到其他代码中。Express本身是一个模块,我们在Express应用程序中使用的中间件和数据库库也是如此。

下面的代码展示了我们如何通过模块名称来导入模块,用Express框架作为示例。首先,我们调用require()函数,用字符串('express')指定模块的名称,并调用返回的对象来创建 Express 应用程序 。然后我们就可以访问应用程序对象的属性和函数。

var express = require('express');
var app = express();

您还可以创建自己的模块,这些模块可以以相同的方式导入。

Tip: 您将希望创建自己的模块,因为这允许您将代码组织成可管理的部分——一个单一的单文件应用程序很难理解和维护。使用模块还可以帮助您管理命名空间,因为只有当您使用模块时,您显式导出的变量才会导入。

要在模块之外提供对象,您只需将它们分配给exports对象即可。例如 下面的 square.js 模块是一个导出了area() 和 perimeter()方法的文件:

exports.area = function(width) { return width * width; };
exports.perimeter = function(width) { return 4 * width; };

我们可以使用 require() 导入这个模块,然后调用导出的方法,如下所示:

var square = require('./square'); // Here we require() the name of the file without the (optional) .js file extension
console.log('The area of a square with a width of 4 is ' + square.area(4));

Note: 您还可以为模块指定一条绝对路径(或名称,就像我们最初所做的那样)。

如果您想一次性导出一个完整的对象,而不是每次只构建一个属性,那么将它像下面这样赋值给module.exports。(您也可以这样做,使导出对象的根为构造函数或其他函数):

module.exports = {
  area: function(width) {
    return width * width;
  },
       
  perimeter: function(width) {
    return 4 * width;
  }
};

有关模块的更多信息,请参阅Modules(Node API文档)。

使用异步 APIs

JavaScript代码经常使用异步而不是同步 API 来进行操作,这些操作可能需要一些时间才能完成。同步API是一种每个操作必须在下一次操作开始之前完成的API。例如,下列日志函数是同步的,并按顺序将文本打印到控制台(第一、第二)。

console.log('First');
console.log('Second');

相比之下,异步API是启动一个操作并立即返回(在操作完成之前)的一种API。一旦操作完成,API将使用某种机制来执行附加的操作。例如,下面的代码将打印出“Second, First”,因为即使 setTimeout() 方法首先被调用,并且立即返回,操作也不会在几秒钟内完成。

setTimeout(function() {
   console.log('First');
   }, 3000);
console.log('Second');

Using non-blocking asynchronous APIs is even more important on Node than in the browser, because Node is a single threaded event-driven execution environment. "single threaded" means that all requests to the server are run on the same thread (rather than being spawned off into separate processes). This model is extremely efficient in terms of speed and server resources, but it does mean that if any of your functions call synchronous methods that take a long time to complete, they will block not just the current request, but every other request being handled by your web application.

There are a number of ways for an asynchronous API to notify your application that it has completed. The most common way is to register a callback function when you invoke the asynchronous API, that will be called back when the operation completes. This is the approach used above.

Tip: Using callbacks can be quite "messy" if you have a sequence of dependent asynchronous operations that must be performed in order, because this results in multiple levels of nested callbacks. This problem is commonly known as "callback hell". This problem can be reduced by good coding practices (see http://callbackhell.com/), using a module like async, or even moving to ES6 features like Promises.

 

Note: Node和Express的一个常见约定是使用错误优先回调。在这个约定中,回调函数中的第一个值是一个错误值,而后续的参数则包含成功数据。有一个很好的解释为什么这个方法在这个博客中是有用的:The Node.js Way - Understanding Error-First Callbacks (fredkschott.com).

Creating route handlers

In our Hello World Express example see above we defined a (callback) route handler function for HTTP GET requests to the site root ('/').

app.get('/', function(req, res) {
  res.send('Hello World!');
});

The callback function takes a request and a response object as arguments. In this case the method simply calls send() on the response to return the string "Hello World!" There are a number of other response methods for ending the request/response cycle, for example you could call res.json() to send a JSON response or res.sendFile() to send a file.

JavaScript tip: You can use any argument names you like in the callback functions; when the callback is invoked the first argument will always be the request and the second will always be the response. It makes sense to name them such that you can identify the object you're working with in the body of the callback.

The Express application object also provides methods to define route handlers for all the other HTTP verbs, which are mostly used in exactly the same way: post(), put(), delete(), options(), trace(), copy(), lock(), mkcol(), move(), purge(), propfind(), proppatch(), unlock(), report(), ​​​​​​ mkactivity(), checkout(), merge(), m-search(), notify(), subscribe(), unsubscribe(), patch(), search(), and connect().

There is a special routing method, app.all(), which will be called in response to any HTTP method. This is used for loading middleware functions at a particular path for all request methods. The following example (from the Express documentation) shows a handler that will be executed for requests to /secret irrespective of the HTTP verb used (provided it is supported by the http module).

app.all('/secret', function(req, res, next) {
  console.log('Accessing the secret section ...');
  next(); // pass control to the next handler
});

Routes allow you to match particular patterns of characters in a URL, and extract some values from the URL and pass them as parameters to the route handler (as attributes of the request object passed as a parameter).

Often it is useful to group route handlers for a particular part of a site together and access them using a common route-prefix (e.g. a site with a Wiki might have all wiki-related routes in one file and have them accessed with a route prefix of /wiki/). In Express this is achieved by using the express.Router object. For example, we can create our wiki route in a module named wiki.js, and then export the Router object, as shown below:

// wiki.js - Wiki route module

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

// Home page route
router.get('/', function(req, res) {
  res.send('Wiki home page');
});

// About page route
router.get('/about', function(req, res) {
  res.send('About this wiki');
});

module.exports = router;

Note: Adding routes to the Router object is just like adding routes to the app object (as shown previously).

To use the router in our main app file we would then require() the route module (wiki.js), then call use() on the Express application to add the Router to the middleware handling path. The two routes will then be accessible from /wiki/ and /wiki/about/.

var wiki = require('./wiki.js');
// ...
app.use('/wiki', wiki);

We'll show you a lot more about working with routes, and in particular about using the Router, later on in the linked section Routes and controllers .

Using middleware

Middleware is used extensively in Express apps, for tasks from serving static files to error handling, to compressing HTTP responses. Whereas route functions end the HTTP request-response cycle by returning some response to the HTTP client, middleware functions typically perform some operation on the request or response and then call the next function in the "stack", which might be more middleware or a route handler. The order that middleware is called is up to the app developer.

Note: The middleware can perform any operation, execute any code, make changes to the request and response object, and it can also end the request-response cycle. If it does not then end the cycle it must call next() to pass control to the next middleware function (or the request will be left hanging).

Most apps will use third-party middleware in order to simplify common web development tasks like working with cookies, sessions, user authentication, accessing request POST and JSON data, logging, etc. You can find a list of middleware packages maintained by the Express team (which also includes other popular 3rd party packages). Other Express packages are available on the NPM package manager.

To use third party middleware you first need to install it into your app using NPM. For example, to install the morgan HTTP request logger middleware, you'd do this:

$ npm install morgan

You could then call use() on the Express application object to add the middleware to the stack:

var express = require('express');
var logger = require('morgan');
var app = express();
app.use(logger('dev'));
...

Note: Middleware and routing functions are called in the order that they are declared. For some middleware the order is important (for example if session middleware depends on cookie middleware, then the cookie handler must be added first). It is almost always the case that middleware is called before setting routes, or your route handlers will not have access to functionality added by your middleware.

You can write your own middleware functions, and you are likely to have to do so (if only to create error handling code). The only difference between a middleware function and a route handler callback is that middleware functions have a third argument next, which middleware functions are expected to call if they do not complete the request cycle (when the middleware function is called, this contains the next function that must be called).

You can add a middleware function to the processing chain with either app.use() or app.add(), depending on whether you want to apply the middleware to all responses or to responses with a particular HTTP verb (GET, POST, etc). You specify routes the same in both cases, though the route is optional when calling app.use().

The example below shows how you can add the middleware function using both methods, and with/without a route.

var express = require('express');
var app = express();

// An example middleware function
var a_middleware_function = function(req, res, next) {
  // ... perform some operations
  next(); // Call next() so Express will call the next middleware function in the chain.
}

// Function added with use() for all routes and verbs
app.use(a_middleware_function);

// Function added with use() for a specific route
app.use('/someroute', a_middleware_function);

// A middleware function added for a specific HTTP verb and route
app.get('/', a_middleware_function);

app.listen(3000);

JavaScript Tip: Above we declare the middleware function separately and then set it as the callback. In our previous route handler function we declared the callback function when it was used. In JavaScript, either approach is valid.

The Express documentation has a lot more excellent documentation about using and writing Express middleware.

Serving static files

You can use the express.static middleware to serve static files, including your images, CSS and JavaScript (static() is the only middleware function that is actually part of Express). For example, you would use the line below to serve images, CSS files, and JavaScript files from a directory named 'public' at the same level as where you call node:

app.use(express.static('public'));

Any files in the public directory are served by adding their filename (relative to the base "public" directory) to the base URL. So for example:

http://localhost:3000/images/dog.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/about.html

You can call static() multiple times to serve multiple directories. If a file cannot be found by one middleware function then it will simply be passed on to the subsequent middleware (the order that middleware is called is based on your declaration order).

app.use(express.static('public'));
app.use(express.static('media'));

You can also create a virtual prefix for your static URLs, rather than having the files added to the base URL. For example, here we specify a mount path so that the files are loaded with the prefix "/media":

app.use('/media', express.static('public'));

Now, you can load the files that are in the public directory from the /media path prefix.

http://localhost:3000/media/images/dog.jpg
http://localhost:3000/media/video/cat.mp4
http://localhost:3000/media/cry.mp3

For more information, see Serving static files in Express.

Handling errors

Errors are handled by one or more special middleware functions that have four arguments, instead of the usual three: (err, req, res, next). For example:

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

These can return any content required, but must be called after all other app.use() and routes calls so that they are the last middleware in the request handling process!

Express comes with a built-in error handler, which takes care of any remaining errors that might be encountered in the app. This default error-handling middleware function is added at the end of the middleware function stack. If you pass an error to next() and you do not handle it in an error handler, it will be handled by the built-in error handler; the error will be written to the client with the stack trace.

Note: The stack trace is not included in the production environment. To run it in production mode you need to set the the environment variable NODE_ENV to 'production'.

Note: HTTP404 and other "error" status codes are not treated as errors. If you want to handle these, you can add a middleware function to do so. For more information see the FAQ.

For more information see Error handling (Express docs).

Using databases

Express apps can use any database mechanism supported by Node (Express itself doesn't define any specific additional behaviour/requirements for database management). There are many options, including PostgreSQL, MySQL, Redis, SQLite, MongoDB, etc.

In order to use these you have to first install the database driver using NPM. For example, to install the driver for the popular NoSQL MongoDB you would use the command:

$ npm install mongodb

The database itself can be installed locally or on a cloud server. In your Express code you require the driver, connect to the database, and then perform create, read, update, and delete (CRUD) operations. The example below (from the Express documentation) shows how you can find "mammal" records using MongoDB.

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/animals', function(err, db) {
  if (err) throw err;

  db.collection('mammals').find().toArray(function (err, result) {
    if (err) throw err;

    console.log(result);
  });
});

Another popular approach is to access your database indirectly, via an Object Relational Mapper ("ORM"). In this approach you define your data as "objects" or "models" and the ORM maps these through to the underlying database format. This approach has the benefit that as a developer you can continue to think in terms of JavaScript objects rather than database semantics, and that there is an obvious place to perform validation and checking of incoming data. We'll talk more about databases in a later article.

For more information see Database integration (Express docs).

Rendering data (views)

Template engines (referred to as "view engines" by Express) allow you to specify the structure of an output document in a template, using placeholders for data that will be filled in when a page is generated. Templates are often used to create HTML, but can also create other types of document. Express has support for a number of template engines, and there is a useful comparison of the more popular engines here: Comparing JavaScript Templating Engines: Jade, Mustache, Dust and More.

In your application settings code you set the template engine to use and the location where Express should look for templates using the 'views' and 'view engines' settings, as shown below (you will also have to install the package containing your template library too!)

var express = require('express');
var app = express();

// Set directory to contain the templates ('views')
app.set('views', path.join(__dirname, 'views'));

// Set view engine to use, in this case 'some_template_engine_name'
app.set('view engine', 'some_template_engine_name');

The appearance of the template will depend on what engine you use. Assuming that you have a template file named "index.<template_extension>" that contains placeholders for data variables named 'title' and "message", you would call Response.render() in a route handler function to create and send the HTML response:

app.get('/', function(req, res) {
  res.render('index', { title: 'About dogs', message: 'Dogs rock!' });
});

For more information see Using template engines with Express (Express docs).

File structure

Express makes no assumptions in terms of structure or what components you use. Routes, views, static files, and other application-specific logic can live in any number of files with any directory structure. While it is perfectly possible to have the whole Express application in one file, typically it makes sense to split your application into files based on function (e.g. account management, blogs, discussion boards) and architectural problem domain (e.g. model, view or controller if you happen to be using an MVC architecture).

In a later topic we'll use the Express Application Generator, which creates a modular app skeleton that we can easily extend for creating web applications.

总结

恭喜,你完成了Express/Node旅程的第一步 !你现在应该了解Express 与 Node的主要优点,并大致了解了Express应用的主要部分看起來的模样 (routes, middleware, error handling, and template code)。你你也应该了解that with Express being an unopinionated framework, the way you pull these parts together and the libraries that you use are largely up to you!

当然,Express被有意设计为一个非常轻量的网页应用框架, so much of its benefit and potential comes from third party libraries and features. 我們將在接下來的文章中,探討更多細節。下一篇文章,我們將探討Node開發環境的配置,因此你可以開始看到一些Express源碼的實際運作。

相关链接

文档标签和贡献者

最后编辑者: codeofjackie,