1. Request parameters

When a client sends a request to the server, it sometimes needs to carry some client information. The client information needs to be passed to the server in the form of request parameters, such as login operations.

2. GET Request parameters

  • Parameters are placed in the browser address bar, for example: http://localhost:3000/? name=zhangsan&age=20
  • Parameter acquisition needs to use the system module URL, URL module is used to process URL address
const http = require('http');
// Import url system module for processing URL addresses
const url = require('url');
const app = http.createServer();
app.on('request'.(req, res) = > {
    // Parse out parts of the URL path and return objects
    // true means to parse the argument into an object format
    let {query} = url.parse(req.url, true);
    console.log(query);
});
app.listen(3000);
Copy the code

3. POST request parameters

  • Parameters are placed in the request body for transmission
  • The DATA event and end event are used to get the POST parameter
  • Convert parameters to object format using the QueryString system module
Const queryString = require(' queryString ');
app.on('request'.(req, res) = > {
    let postData = ' ';
    // Listen for parameter transfer events
    req.on('data'.(chunk) = >postData += chunk;) ;// Listen for the parameter transfer completion event
    req.on('end'.() = > {
        console.log(querystring.parse(postData));
    });
});
Copy the code

4. The routing

  • http://localhost:3000/index
  • http://localhost:3000/login
  • Routing refers to the mapping between the client request address and the server-side program code. Simply put, it means responding to what is requested.

// When the client sends a request
app.on('request'.(req, res) = > {
    // Get the client request path
    let { pathname } = url.parse(req.url);
    if (pathname == '/' || pathname == '/index') {
        res.end('Welcome to the home page');
    } else if (pathname == '/list') {
        res.end('Welcome to the list page');
    } else {
        res.end('Sorry, the page you visited is gone.'); }});Copy the code

4.1. The third-party module Router

Function: Realize routing

Use steps:

  1. Getting a routing object
  2. Create a route by calling the method provided by the route object
  3. Enable the route to take effect
const getRouter = require('router')
const router = getRouter();
router.get('/add'.(req, res) = > {
    res.end('Hello World! ')
})
server.on('request'.(req, res) = > {
    router(req, res)
})
Copy the code

Static resources

The server does not need to process the resources that can directly respond to the client are static resources, such as CSS, JavaScript, image files. www.itcast.cn/images/logo…

5.1. The third-party serve-static module

Function: static resource access service

Steps:

  1. The serve-static module is introduced to obtain the method of creating static resource services
  2. Calls the method to create a static resource service and specify a static resource service directory
  3. Enable the static resource service function
const serveStatic = require('serve-static')
const serve = serveStatic('public')
server.on('request'.() = > {
    serve(req, res)
})
server.listen(3000)
Copy the code

Dynamic resources

  • The same request address of a different response resource, this resource is a dynamic resource.
  • www.itcast.cn/article?id=…
  • www.itcast.cn/article?id=…

7. Client request path

  1. The GET method
  • Browser address bar
  • Href attribute of the link tag
  • The SRC attribute of the script tag
  • The SRC attribute of the img tag
  • Form submission
  1. POST way
  • Form submission