node. JS Manual Query-express method
1. Send method
The Send method sends a response message to the browser, and can intelligently handle different types of data that the Send method automatically makes when the response is output, such as head information, HTTP cache support, and so on, such as String, Array, Object, number. When the parameter is a string, Content-type defaults to "text/html" when the parameter is an array or an object, express returns a JSON when the parameter is a numberexpress will help you set a response body, For example: 200
2. Common methods for obtaining parameters
(1), Req.body
(2), req.query
(3), Req.params
(i), req.body example
Body is not Nodejs by default, you need to load Body-parser middleware to use Req.body, this method is usually used to parse the data in the POST request
<form action='/test'Method='Post'> <input type='text'Name='name'Value='LMW'> <input type='text'Name='Tel'Value='1234567'> <input type='Submit'Value='Submit'> </form>App.post ('/test', Function (req, res) {Console.log (req.body.name); Console.log (Req.body.tel);});
(ii), req.query examples
Nodejs is provided by default and does not need to be loaded into the middleware, which is often used to parse the data in a GET request
get/test?name=lmw&tel=123456789app.get('/test' ) , Function (req, res) { console.log (req.query.name); Console.log (Req.query.tel);});
(iii), Req.query and req.body at the same time
<form action='/test?id=1'Method='Post'> <input type='text'Name='name'Value='LMW'> <input type='text'Name='Tel'Value='123456789'> <input type='Submit'Value='Submit'> </form>App.post ('/test', Function (req, res) {Console.log (req.query.id); Console.log (Req.body.name); Console.log (Req.body.tel);});
(iv), Req.params
Another way to pass parameters to the server, but this is not the traditional standard practice, is an extension of the HTTP Routing application
get/test/lmw/123456789app.get('/test/:name/:tel', Function (req, res) { console.log (req.params. Name); Console.log (req.Params. Tel);});
node. JS Common Express method