express 路由中间件_Express通过示例进行解释-安装,路由,中间件等

express 路由中间件

表达 (Express)

When it comes to build web applications using Node.js, creating a server can take a lot of time. Over the years Node.js has matured enough due to the support from community. Using Node.js as a backend for web applications and websites help the developers to start working on their application or product quickly.

在使用Node.js构建Web应用程序时,创建服务器可能需要很多时间。 多年来,由于社区的支持,Node.js已经足够成熟。 使用Node.js作为Web应用程序和网站的后端可以帮助开发人员快速开始开发其应用程序或产品。

In this tutorial, we are going to look into Express which is a Node.js framework for web development that comes with features like routing and rendering and support for REST APIs.

在本教程中,我们将研究Express,这是一个用于Web开发的Node.js框架,具有路由和渲染等功能以及对REST API的支持。

什么是快递? (What is Express?)

Express is the most popular Node.js framework because it requires minimum setup to start an application or an API and is fast, and unopinionated at the same time. In other words, it does not enforces its own philosophy that a application or API should be built in a specific way, unlike Rails and Django. Its flexibility can be calculated by the number of npm modules available which makes it pluggable at the same time. If you have basic knowledge of HTML, CSS, and JavaScript and how Node.js works in general, in no time you will be able to get started with Express.

Express是最流行的Node.js框架,因为它需要最少的设置来启动应用程序或API,并且速度快且同时不受限制。 换句话说,与Rails和Django不同,它没有实施自己的哲学,即应以特定方式构建应用程序或API。 它的灵活性可以通过可用的npm模块数量来计算,这使得它可以同时插入。 如果您具有HTML,CSS和JavaScript的基本知识以及Node.js的一般工作原理,那么您很快就可以开始使用Express。

Express was developed by TJ Holowaychuk and is now maintained by Node.js foundation and open source developers. To get started with the development using Express, you need to have Node.js and npm installed. You can install Node.js on your local machine and along with it comes the command line utility npm that will help us to install plugins or as called dependencies later on in our project.

Express由TJ Holowaychuk开发,现在由Node.js基金会和开源开发人员维护。 要开始使用Express进行开发,您需要安装Node.js和npm。 您可以在本地计算机上安装Node.js ,并附带命令行实用程序npm ,它将帮助我们稍后在项目中安装插件或称为依赖项。

To check if everything is installed correctly, please open your terminal and type:

要检查所有内容是否正确安装,请打开终端并输入:

node --version
v5.0.0
npm --version
3.5.2

If you are getting the version number instead of an error that means you have installed Node.js and npm successfully.

如果获取的是版本号而不是错误,则表示您已成功安装Node.js和npm。

为什么要使用Express? (Why use Express?)

Before we start with mechanism of using Express as the backend framework, let us first explore why we should consider it using or the reasons of its popularity.

在开始使用Express作为后端框架的机制之前,让我们首先探讨为什么应该考虑使用Express或其流行的原因。

  • Express lets you build single page, multi-page, and hybrid web and mobile applications. Other common backend use is to provide an API for a client (whether web or mobile).

    Express使您可以构建单页,多页以及混合的Web和移动应用程序。 后端的其他常见用法是为客户端(无论是Web还是移动设备)提供API。
  • It comes with a default template engine, Jade which helps to facilitate the flow of data into a website structure and does support other template engines.

    它带有一个默认的模板引擎Jade,它有助于促进数据流到网站结构中,并且确实支持其他模板引擎。
  • It supports MVC (Model-View-Controller), a very common architecture to design web applications.

    它支持MVC(模型-视图-控制器),这是设计Web应用程序的非常常见的体系结构。
  • It is cross-platform and is not limited to any particular operating system.

    它是跨平台的,不限于任何特定的操作系统。
  • It leverages upon Node.js single threaded and asynchronous model.

    它利用Node.js单线程和异步模型。

Whenever we create a project using npm, our project must have a package.json file.

每当我们使用npm创建项目时,我们的项目都必须具有package.json文件。

创建package.json (Creating package.json)

A JSON (JavaScript Object Notation) file is contains every information about any Express project. The number of modules installed, the name of the project, the version, and other meta information. To add Express as a module in our project, first we need to create a project directory and then create a package.json file.

JSON(JavaScript对象表示法)文件包含有关任何Express项目的所有信息。 安装的模块数量,项目名称,版本和其他元信息。 要将Express作为模块添加到我们的项目中,首先我们需要创建一个项目目录,然后创建一个package.json文件。

mkdir express-app-example
cd express-app-example
npm init --yes

This will generate a package.json file in the root of the project directory. To install any module from npm we need to have package.json file exist in that directory.

这将在项目目录的根目录中生成一个package.json文件。 要从npm安装任何模块,我们需要在该目录中存在package.json文件。

{
  "name": "express-web-app",
  "version": "0.1.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "license": "MIT"
}

安装Express (Installing Express)

Now we have package.json file, we can install Express by running the command:

现在我们有了package.json文件,我们可以通过运行以下命令来安装Express:

npm install --save express

We can confirm that Express has correctly installed by two ways. First, there will be new section in package.json file named dependencies under which our Express exists:

我们可以通过两种方式确认Express是否已正确安装。 首先, package.json文件中将有一个名为dependencies新部分,我们的Express存在于此部分:

{
  "name": "express-web-app",
  "version": "0.1.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "license": "MIT",
  "dependencies": {
    "express": "4.16.0"
  }
}

Second way is that a new folder called node_modules suddenly appeared in the root of our project directory. This folder stores the packages we install locally in our project.

第二种方法是在我们的项目目录的根目录中突然出现一个名为node_modules的新文件夹。 此文件夹存储我们在项目中本地安装的软件包。

使用Express构建服务器 (Building a Server with Express)

To use our installed package for Express framework and create a simple server application, we w

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值