【翻译】Express web应用开发 第一章
本章节是一个对初学者友好的Express介绍。你将学习到Express的基础知识、核心概念和实现一个Express应用的组成部分。现阶段我们不需要做太多的编码,本章节会让你熟悉和习惯Express,为接下来的章节做好准备。
别跳过这一章节,这篇材料为你的Express学习之旅提供了指南。
什么是Express?
Express是一个轻量、灵活、强大的NodeJS web开发框架。
What do we mean by minimal yet flexible and powerful?
Express is minimal because it does not come loaded with all sorts of functionality,
which makes it a bloat-free framework. Out of the box, it supports only the very
basic features of a web framework. Even the supported features are not all enabled
by default, you have the option to pick and use, according to your needs.
The flexibility in Express comes from the use of middlewares and Node modules.
Express middlewares and Node modules are pluggable JavaScript components,
which make Express apps very modular, flexible, and extensible.
Express is a powerful framework because it gives you complete access to the core
Node APIs. Anything you can do with Node, you can do it with Express too.
Express can be used to create very simple to very complex web apps. It provides
you all the tools required to create the most complex of apps, but does not force
you to use them when you don't need them.
What is Express?
[6 ]
Hearing someone tell you that Express is a minimal, flexible, and powerful web
development framework doesn't really help much in understanding it, does it?.
Many other frameworks probably claim the same thing. Let's find out what is
actually special about Express.
安装Express
如果你安装了NodeJS,安装Express就非常简单。
Express是一个Node模块,和其他Node模块一样,我们可以通过NPM(Node Package Manager)安装,NPM在安装Node的时候已经默认安装。后面会详细介绍NPM和Node模块。
Node模块两种引入形式:local和global。local模块意味着在特定的项目中使用,只对该项目可用。而global模块安装到全局,基本上都随时可以通过命令行工具使用。
Express将被安装到全局,这样我们就可以通过express命令行工具快速初始化Express项目。
【tip:Express是web应用开发框架,express是创建Express应用框架的命令行工具】
我们通过在npm install命令中制定 -g选项来将Node模块安装到全局。安装Express命令如下:
npm install express -g
这个命令将安装最新稳定版本的Express。如果你想安装一个特定版本的Express,你可以通过@参数在模块名称中指定特定的版本号。例如:
npm install express@3.0.5 -g
安装完毕后,可以通过检查版本号确定我们已经可以使用express命令:
express -v
恭喜!现在,你的系统已经可以进行Express开发了!
Express组成
好消息是,Express只有3个核心组件,如果不需要彻底掌握,这就可以帮助我们了解Express很多功能。这一节我们将简单介绍Express的每个核心模块,在后面的章节我们遇到这些核心模块时就不会毫无头绪。
application对象
application对象是Express的一个实例,通常用app变量取代。这是Express应用的主要对象,大部分功能都建立在它的基础之上。
下面是怎样创建一个Express实例:
var express = require('express');
var app = new express();
下面是对app对象的属性和方法的介绍:
| Property/Method | Description |
| app.set(name, value) | Sets app-specific properties |
| app.get(name) | Retrieves value set by app.set() |
| app.enable(name) | Enables a setting in the app |
| app.disable(name) | Disables a setting in the app |
| app.enabled(name) | Checks if a setting is enabled |
| app.disabled(name) | Checks if a setting is disabled |
| app.configure([env], callback) | Sets app settings conditionally based on the development environment |
| app.use([path], function) | Loads a middleware in the app |
| app.engine(ext, callback) | Registers a template engine for the app |
| app.param([name], callback) | Adds logic to route parameters |
| app.VERB(path, [callback...],callback) | Defines routes and handlers based on HTTP verbs |
| app.all(path, [callback...], callback) | Defines routes and handlers for all HTTP verbs |
| app.locals | The object to store variables accessible from any view |
| app.render(view, [options], callback) | Renders view from the app |
| app.routes | A list of routes defined in the app |
| app.listen() | Binds and listen for connections |
request对象
当客户端向Express应用发起请求时,Http request对象将被创建。request对象包含一系列与当期请求相关的属性和变量,通常被变量req取代。
Property/Method Description
req.params Holds the values of named routes parameters
req.params(name) Returns the value of a parameter from named routes or GETparams or POST params
req.query Holds the values of a GETform submission
req.body Holds the values of a POSTform submission
req.files Holds the files uploaded via a form
req.route Provides details about the current matched route
req.cookies Cookie values
req.signedCookies Signed cookie values
req.get(header) Gets the request HTTP header
req.accepts(types) Checks if the client accepts the media types
req.accepted A list of accepted media types by the client
req.is(type) Checks if the incoming request is of the particular media type
req.ip The IP address of the client
req.ips The IP address of the client, along with that of the proxies it is connected through
req.path The request path
req.host Hostname from the HTTP header
req.fresh Checks if the request is still fresh
req.stale Checks if the request is stale
req.xhr Checks if the request came via an AJAX request
req.protocol The protocol used for making the request
req.secure Checks if it is a secure connection
req.subdomains Subdomains of the host domain name
req.url The request path, along with any query parameters
req.originalUrl Used as a backup for req.url
req.acceptedLanguages A list of accepted languages by the client
req.acceptsLanguage(langauge) Checks if the client accepts the language
req.acceptedCharsets A list of accepted charsets by the client
req.acceptsCharsets(charset) Checks if the client accepts the charset
response对象
累了。。歇歇。翻译真是个体力活
【翻译】Express web应用开发 第一章的更多相关文章
- 【翻译习作】 Windows Workflow Foundation程序开发-第一章04
1.2.3 Windows Workflow运行时 从Windows Workflow的角度看,可以将工作流活动当成是交给一个工作流处理器去执行的一系列指令或操作码.在Windows Workflo ...
- 【翻译习作】 Windows Workflow Foundation程序开发-第一章05
1.3 开发我们的第一个工作流 也许你曾经在这样的产品经理手下搞过开发:他总是在你身边转悠,并不时的问一句“你还没做完吗?”.在这一部分,我们将用一个简单的Windows Workflow程 ...
- 【翻译习作】 Windows Workflow Foundation程序开发-第一章03
1.2.2.Visual Studio 2005扩展包 微软也为Windows Workflow开发者提供了Visual Studio 2005扩展包.扩展包将许多功能集成到Visual Studio ...
- 【翻译习作】 Windows Workflow Foundation程序开发-第一章02
1.2 Windows Workflow概览 微软的Windows Workflow Foundation(简称WF)是.NET框架3.0版的一部分..NET3.0其它主要部分是Window ...
- ASP.NET自定义控件组件开发 第一章 第三篇
原文:ASP.NET自定义控件组件开发 第一章 第三篇 第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待 ...
- ASP.NET自定义控件组件开发 第一章 第二篇 接着待续
原文:ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 很感谢大家给我的第一篇ASP.NET控件开发的支持!在写这些之前,我也看了 ...
- ASP.NET自定义控件组件开发 第一章 待续
原文:ASP.NET自定义控件组件开发 第一章 待续 第一章:从一个简单的控件谈起 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接 ...
- ASP.NET自定义控件组件开发 第一章 第三篇 第一章的完结篇
ASP.NET自定义控件组件开发 第一章 第三篇 第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ...
- ASP.NET自定义控件组件开发 第一章 第一章:从一个简单的控件谈起
第一章:从一个简单的控件谈起 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第三 ...
随机推荐
- Unity3D 解决用Unity导出的Android工程在6.0及以上设备会弹出一串权限对话框的问题
解决用Unity导出的Android工程在6.0及以上设备会弹出一串权限对话框的问题 <meta-data android:name="unityplayer.SkipPermissi ...
- 2015 史考特(Scottrade)开户指南 + 招商银行香港一卡通汇款【图文教程】
最近刚开始炒美股.总的来说分为两步:一是开户,即选一个美股券商开设股票交易账户:二是汇款注资,把人民币换成美元转账到股票交易账户上.上述第一点其实相对简单,美股券商大多都对美国以外的外国人开放申请,且 ...
- 在WebPart中获取Office 365中的未读邮件数
// Create the web request HttpWebRequest request = WebRequest.Create("https://outlook.office365 ...
- Alipay秘钥问题
有三种秘钥一个是应用公钥 一个是支付宝公钥 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } span.s1 { text-d ...
- 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)
前些日子工作不太忙,自己开发了一个web框架用于以后快速开发,现在分享出来. 系统没有使用各种复杂的东西,也没有太多的层次,有兴趣的可以研究一下.
- 基于EasyUI Treegrid的权限管理资源列表
1. 前言 最近在开发系统权限管理相关的功能,主要包含用户管理,资源管理,角色管理,组类别管理等小的模块.之前的Web开发中也用过jQueryEasyUI插件,感觉这款插件简单易用,上手很快.以前用到 ...
- 《ASP.NET MVC高级编程(4版)》读书笔记(5)表单和HTML辅助方法
5.1 表单使用 5.1.1 action 和 method 特性 <form action="/Home/Index"> <input name=&qu ...
- 使用PowerShell来修改文件访问,创建,修改时间属性
Function Set-FileTimeStamps { Param ( [Parameter(mandatory=$true)] [string[]]$path, [datetime]$date ...
- Spring-boot 开发Web应用
动态修改Freemarker模版: 设置模版属性: spring.freemarker.cache=false 启动应用方式有两种: a. 运行main()函数启动应用:则修改完模版文件后,需要把 ...
- eclipse启动不了,出现“Java was started but returned exit code=13......”对话框
eclipse启动不了,出现"Java was started but returned exit code=13......"对话框如下 解决方案:1.使用的是java jdk6 ...