文件名称:
express-handbook.pdf
开发工具:
文件大小: 1mb
下载次数: 0
上传时间: 2019-07-04
详细说明:一份很好的Express英文文档,帮助你更快的学习和梳理Express知识The Express Handbook
The Express Handbook
The Express Handbook follows the 80/20 rule: learn in 20%of the time the 80% of a topic
I find this approach gives a well-rounded overview. This book does not try to cover everything
under the sun related to Express. If you think some specific topic should be included, tell me
You can reach me on Twitter flaviocopes
I hope the contents of this book will help you achieve what you want: learn the basics
Express
This book is written by Flavio. I publish web development tutorials every day on my website
flaviocopes. com
Enjoy
Express overview
Express overview
Express is a Node. js Web Framework. Node js is an amazing tool for building
networking services and applications. Express builds on top of its features to
provide easy to use functionality that satisfy the needs of the Web server use
case
EXPKESS
Express is a Node. js Web Framework
Node js is an amazing tool for building networking services and applications
Express builds on top of its features to provide easy to use functionality that satisfy the needs
of the web server use case
It's Open Source, free, easy to extend, very performant, and has lots and lots of pre-built
packages you can just drop in and use, to perform all kind of things
Installation
You can install Express into any project with npm
Express overview
npm install express --save
or yarn
yarn add express
Both commands will also work in an empty directory, to start up your project from scratch
although npm does not create a package, json file at all, and Yarn creates a basic one
Just run npm init or yarn init if you re starting a new project from scratch
Hello world
We're ready to create our first Express Web Server
Here is some code
const express require('express)
const app = express()
app. get(/,(reg, res)=> res send( Hello world! )
app. listen(3000,(=> console. log( Server ready )
Save this to an index. js file in your project root folder, and start the server using
node index. js
You can open the browser to port 3000 on localhost and you should see the Hello world!
message.
Learn the basics of Express by understanding
the hello world code
Those 4 lines of code do a lot behind the scenes
First, we import the express package to the express value
We instantiate an application by calling its app() method
Once we have the application object, we tell it to listen for Get requests on the path, using
the get() method
Express overview
There is a method for every Http verb get(), post(), put(), delete(), patch()
app. get(/(
res)=> t
app. post(/,(reg, res)=>[/**/])
app. put(
)=>{/*x/1)
)=>{/**/})
app. patch(/,(req, res)=>[/x*/3)
Those methods accept a callback function, which is called when a request is started, and we
need to handle it
We pass in an arrow function
(reg, res)=> res send( Hello World!
Express sends us two objects in this callback, which we called req and res, that represent
the Request and the Response objects
RequestisthehttprequestItcangiveusalltheinfoaboutthatincludingtherequest
parameters, the headers, the body of the request, and more
Responseisthehttpresponseobjectthatwellsendtotheclient
What we do in this callback is to send the Hello world! string to the client, using the
Response. send()method
This method sets that string as the body, and it closes the connection
The last line of the example actually starts the server, and tells it to listen on port 3000.We
pass in a callback that is called when the server is ready to accept new requests
Request parameters
Request parameters
a handy reference to all the request object properties and how to use them
Request parameters
I mentioned how the request object holds all the Http request information
These are the main properties you'll likely use
Property
Description
app
holds a reference to the Express app object
baseUrl
the base path on which the app responds
contains the data submitted in the request body(must be parsed and
populated manually before you can access it
contains the cookies sent by the request (needs the cookie-parser
cookIes
middleware)
hostname
the server hostname
. Ip
the server P
method
thehttpmethodused
params
the route named parameters
path
the Url path
protocol
the request protocol
query
an object containing all the query strings used in the request
secure
true if the request is secure(uses Https
signed Cookies
contains the signed cookies sent by the request(needs the cookie
parser middleware
true if the request is an XmlhTtpreqUest
How to retrieve the get query string parameters
using Express
The query string is the part that comes after the URl path and starts with a question mark
Example:
Request parameters
?name=flavio
Multiple query parameters can be added using
?name=flavio&age=35
How do you get those query string values in Express?
Express makes it very easy by populating the Request query object for us
const express require(express)
const app express()
app. get(/,(req res)=> i
console. log (req. query)
app. listen (8080)
This object is filled with a property for each query parameter
If there are no query params, it's an empty object
This makes it easy to iterate on it using the for. in loop
for (const key in reg. query)i
console. log(key, reg. query lkey ])
This will print the query property key and the value
You can access single properties as well
req. query name //flavio
req. query age //35
How to retrieve the POST query string
parameters using Express
Post query parameters are sent by Http clients for example by forms or when performing a
POST request sending data
How can you access this data?
Request parameters
If the data was sent as JSON, using Content-Type: application/json, you will use the
express json( middleware
const express require(express)
const app express(
app. use(express.]son())
If the data was sent as JSoN, using Content-Type: application/x-Www-form-urlencoded, you will
use the express urlencode( middleware
const express require(express)
const app expresso
app. use(express urlencode()
In both cases you can access the data by referencing it from Request body
app. post( /form,(reg, res)=>t
const name = req. body.name
Note: older Express versions required the use of the body-parser module to process
POST data. This is no longer the case as of Express 4. 16(released in September 2017)
nd later versions
Sending a response
Sending a response
How to send a response back to the client using Express
In the Hello World example we used the Response. send() method to send a simple string as a
response, and to close the connection
(reg, res)=> res send('Hello world!
If you pass in a string, it sets the Content-Type header to text/html
if you pass in an object or an array, it sets the application/json Content-Type header, and
parses that parameter into JSON
send()automatically sets the Content-length Http response header
send()also automatically closes the connection
Use end to send an empty response
An alternative way to send the response, without any body, it's by using the Response. end(
method
esend()
Set the Http response status
Use the Response status()
res status(404).endo
res status(404), send( File not found
sendstatus(is a shortcut
res. sendstatus(200)
//=== res status(200). send('OK)
res. sendstatus(403
//=== res, status(403). send('Forbidden
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.