Node.js- Express framework

A, Express Profile

1.Express is a simple and flexible node.jsWeb application framework that provides powerful features to help you create a variety of web applications, and rich http tool.

2. Express can quickly build a fully functional website.

3.Express framework core features

1.1 may be provided in response to the HTTP request to the middleware

1.2 definitions different routing tables for performing an operation of the HTTP request

1.3 can be dynamically rendered HTML page templates to pass parameters

Second, install Express

 

 

 

The above command will install the Express framework node_modules the current directory, the directory will be created automatically express node_modules directory.

Several important module is the need to express frame mounted with:

1. body-parser - node.js middleware, JSON, Raw, Text and URL-encoded data for processing.

2. cookie-parser - This is a tool for parsing cookie. By req.cookies can go to pass over the cookie, and turn them into objects.

3. multer - node.js middleware, enctype = "multipart / form-data" (a form of MIME encoding is provided) of the form data for processing.

 

 

 

 

Three, Express instance

 

 

 

 

 

 

Implementation of the results:

 

 

 

 

 

Fourth, the request and response

express application callback function parameters, request and response objects to process data requests and responses.

 

app.get('/', function(req, res){

// --

}) 

Introduce specific request and response objects

1. Request Object - request object represents an HTTP request, to request containing query string parameter, contents, HTTP header attributes.

Common attributes are:

1.1 req.app: When the callback as an external file, access instances express with req.app

1.2 req.baseUrl: get URL routing currently installed

1.3 req.body / req.cookies: get the request body] [/ Cookies

1.4 req.fresh / req.stale: Fresh] [No further requests is determined

1.5 req.hostname / req.ip: Get names and IP addresses

1.6 req.originalUrl: get the original request URL

1.7 req.params: obtaining routing parameters

1.8 req.path: acquiring request path

1.9 req.protocal: Gets the protocol type

1.10 req.query: get the URL query string parameters

1.11 req.route: Get the current matching route

1.12 req.subdomains: obtaining sub-domain

1.13 req.accepts (): checking the type of document may be received request

1.14 req.acceptsChasets / req.acceptsEncodings / req.acceptsLanguages: Returns the specified character set of a character encoding acceptable

1.15 req.get (): Get the specified HTTP request header

1.16 req.is (): if the request Content-Type header MIME type

2.Response Object - response object represents the HTTP response, HTTP that is sent to the client upon receiving the request response data. Common attributes are:

2.1 res.app: same as req.app

2.2 res.append (): adding the specified HTTP header

2.3 res.set () before resetting the head after res.append () provided

2.4 res.cookie(name, value[,option]): 设置Cookie

2.5 option: domain/expires/ httpOnly/maxAge/path/secure/ signed

2.6 res.clearCookie(): 清除Cookie

2.7 res.download (): file transfer specified path

2.8 res.get (): Returns the specified HTTP header

2.9 res.json (): transfer in response JSON

2.10 res.jsonp (): transfer JSONP response

2.11 res.location (): Set Location HTTP response header only, is not provided or close response status code

2.12 res.redirect (): Set Location HTTP response header, and sets the status code 302

2.13 res.render (view, [locals], callback): rendering a view, while the rendered string is transmitted to the callback, if an error occurs next (err) during the rendering process will be automatically invoked. callback will be passed in a possible error occurred and after rendering the page, so it will not automatically output.

2.14 res.send (): sends an HTTP response

2.15 res.sendFile (path [, optins] [, fn]): Specifies the transmission path file - are automatically set based on the file extension Content-Type

2.16 res.set (): Set HTTP header, passing object may be provided a plurality of heads

2.17 res.status (): Set HTTP status code

2.18 res.type (): Set Content-Type of the MIME type

Guess you like

Origin www.cnblogs.com/plBlog/p/11431387.html
Recommended