Talking about Express Framework (1)

Express core features:

 1. Middleware can be set to respond to HTTP requests.

 2. A routing table is defined for executing different HTTP requests.

 3. HTML can be rendered dynamically by passing parameters through templates.

Note: express() is an entry function exported by the express module var app = express();

Common method:

  1.app.set(name,value) Set the value of the setting item name to value.

  2.app.get(name) Get the value of the setting item name.

  3.app.use([path],function) Use the middleware function, the optional parameter path is "/" by default.

  4.app.listen(port number) Listen for requests on the given host and port.

  5.app.engine(ext,callback) Register the template engine, the first parameter; the name of the template engine, which is also the suffix of the template file, and the second parameter indicates the method used to parse and process the template content.

routing:

  1.app.get(path, route handle) The first parameter is the requested path, and the second parameter is the request processing function.

  2.app.post(path, route handle) The first parameter is the path of the request, and the second parameter is the processing function of the request.

Note: 1. The routing path and the request method together define the endpoint of the request, which can be a string, a string pattern or a regular expression.

        2. The routing handle can provide multiple callback functions for request processing, which behave like middleware. The only difference is that it is possible for these callbacks to call  next('route') methods that bypass other routed callbacks. This mechanism can be used to define preconditions for a route, and if it doesn't make sense to continue on an existing path, control can be passed on to the remaining paths. Route handles come in many forms and can be a function, an array of functions, or a mix of both.

Response object (res):

Note: The response object represents the HTTP response, that is, the HTTP response data sent to the client when the request is received.

  1.res.render() renders the template.

  2.res.json() Send a response in JSON format.

  3.res.jsonp() Send a response in JSON format that supports JSONP.

  4.res.send() Send various types of responses.

  5.res.set() Set the HTTP header, and the incoming object can set multiple headers at a time.

  6.res.cookie(name,value,[,options])  设置cookie。

Request object (req):

Note: The request object represents an HTTP request, including the request query string, parameters, content, HTTP headers and other attributes.

  1.req.body Get the request body.

  2.req.cookies Get cookies.

  3.req.path Get the request path.

  4.req.query Get the query parameter string of the url.

Learning documents:

http://www.expressjs.com.cn/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775427&siteId=291194637