express的英文api,,,,,,,学习express基本上就是学这几个object,知道他们各自的常用方法就ok
https://expressjs.com/zh-cn/4x/api.html#req
==============================================================================================
express()
Creates an Express application. The express()
function is a top-level function exported by the express
module.
var express = require('express');
var app = express();
Application
The app
object conventionally denotes the Express application. Create it by calling the top-level express()
function exported by the Express module:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
The app
object has methods for
- Routing HTTP requests; see for example, app.METHOD and app.param.
- Configuring middleware; see app.route.
- Rendering HTML views; see app.render.
- Registering a template engine; see app.engine.
Request
The req
object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
For example:
app.get('/user/:id', function(req, res) {
res.send('user ' + req.params.id);
});
But you could just as well have:
app.get('/user/:id', function(request, response) {
response.send('user ' + request.params.id);
});
Response
The res
object represents the HTTP response that an Express app sends when it gets an HTTP request.
For example:
app.get('/user/:id', function(req, res){
res.send('user ' + req.params.id);
});
Router
A router
object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.
A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method.
The top-level express
object has a Router() method that creates a new router
object.
Once you’ve created a router object, you can add middleware and HTTP method routes (such as get
, put
, post
, and so on) to it just like an application. For example:
// invoked for any requests passed to this router
router.use(function(req, res, next) {
// .. some logic here .. like any other middleware
next();
});
// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
// ..
});
You can then use a router for a particular root URL in this way separating your routes into files or even mini-apps.
// only requests to /calendar/* will be sent to our "router"
app.use('/calendar', router);