Recently after reading the Web "node. JS Construction Station Raiders", the MongoDB operation has a further understanding, in order to further consolidate the knowledge of the database, so use the knowledge to build a simple mongodemo.
After the build has been put on GitHub to share, details please poke Mongodemo source interested in the small partners can see; back to the theme, completed the small project Nodejs backstage development has some small enlightenment, this article on req
The most commonly used parameters to get a summary;
node. JS background Development Basically will encounter the use of Req.param (), Req.params , req.query, req.body Get the parameters of the situation, So what are the differences between them? Detailed analysis of the following:
1. Req.param ()
The method is the most convenient to obtain the parameters, it can be said that the other three properties of the complex; But the Express 4.x API documentation says the method will be deprecated! You can only use the other three req properties to get the parameters in the future. (Ps: I express4.x project using Req.param () will not error, but start the project with a warning prompt)
This method is used as follows:
/user/tobi for/user/:name req.param (' name ')//= ' Tobi '
// ? Name=tobireq.param (' name ')// = "Tobi"// POST name=tobi Req.param (' name ')// = "Tobi"
This method can obtain
1) The parameters transmitted by express router;
2) Address bar parameters;
3) Postt The arguments submitted, such as the value of input in the form, the object value of the Ajax (asynchronous) commit, and so on.
2.req.params
This property can only get "the parameters passed by express router" compared to the Req.param () method, which is worth mentioning: with Req.params, you can also play regular in the Express router .
First look at the simple req.params use:
// GET/USER/TJ Req.params.name // = "TJ"
This is true in the complete code:
var express = require (' Express '); var app = Express ();
Address bar: Localhost:3000/user/tj app.get (function(req, res) { var param = Req.params.name res.send (// Hello World TJ});
Then look at the magical regular usage in the router, enter localhost:3000/file/javascripts/jquery.js in the Address bar, and the "/file/*" setting in the route:
// get/file/javascripts/jquery.jsreq.params[0]// = "Javascripts/jquery.js"
Full code:
var express = require (' Express '); var app = Express ();
Address bar: localhost:3000/file/javascripts/jquery.jsapp.get (function(req, res) { var param = req.params[0]; // javascripts/jquery.js});
PS: If the parameter is not set on the router, the value obtained by Req.params is null object {}
3.Req.query
The most straightforward use of this property is to get directly the parameters passed by the address bar;
// Get/search?q=tobi+ferret req.query.q // = "Tobi Ferret" // Get/shoes?order=desc&shoe[color]=blue&shoe[type]=converse Req.query.order // = "desc" Req.query.shoe.color // = "Blue" Req.query.shoe.type // = "Converse"
Full code:
varExpress = require (' Express ');varApp =Express ();//Address bar: Localhost:3000/search?q=tobi+ferretApp.get ('/search ',function(req, res) {varparam =req.query.q; Res.send (param); //Tobi Ferret});//Address bar: Localhost:3000/shoes?order=desc&shoe[color]=blue&shoe[type]=converseApp.get ('/shoes ',function(req, res) {var_order =Req.query.order; var_color =Req.query.shoe.color; var_type =Req.query.shoe.type; Console.log (_order); //descConsole.log (_color);//BlueConsole.log (_type);//ConverseRes.send (' Hello World '); });
PS: If the address bar does not pass parameters, the value obtained by Req.query is also an empty object {}
4. Req.body
This property is mainly used with the Post method when passing parameters, the use of the most extensive and most common, examples are more (write this part of the most tired have wood). It is necessary to note that when using this property, you must first confirm that there is no import "" in App.js body-parser
, and that the module has been detached from the module in express4.x. The sample code is as follows:
var app = require (' Express ' ) (); var Bodyparser = require (' Body-parser ' ); var multer = require (' multer ' // for parsing Application/json App.use (bodyparser.urlencoded ({extended: true })); // for parsing application/x-www-form-urlencoded App.use (Multer ()); // for parsing multipart/form-data app.post ( '/', function (req, res) {Console.log (req.body); Res.json (req.body);})
The most commonly used req.body are:
4-1 Form Post transfer parameters to background:
The website often uses the form to pass the parameter to the backstage, uses the Req.body to receive the parameter in the express4.x, the complete code is as follows:
<formMethod= "POST"Action= "Add"name= "UserForm" > <inputtype= "text"ID= "Name"name= "name" value= "XQ"class= "Form-control" /> <inputtype= "text"ID= "Age"name= "Age"value= " a" class= "Form-control" /> <inputtype= "text"ID= "Job"name= "Job"value= "Coder" class= "Form-control" /> <inputtype= "text"ID= "Hobby"name= "Hobby"Value= "Run" class= "Form-control" /> <Buttontype= "Submit"class= "Btn btn-primary">Submit Add</Button> </form>
var express = require (' Express '); var router = Express. Router (); Router.route ('/add '). Post (function(req, res) { var userobj = {}; = { name:req.body.name, age:req.body.age, job:req.body.job, hobby:req.body.hobby }; Console.log (userobj); // {name: ' Xq ', Age: ' N ', job: ' Coder ', hobby: ' Run '}});
4-2 jquery Ajax Pass parameters to the background:
Website development of course, the use of asynchronous pass parameters to the background, express4.x is also the Req.body receive asynchronous pass parameters, the complete code is as follows:
var _id = ' 123456 ';
$.post ('/user/delete ', {ID: _id}, function (data) { if (Data.error) {$ ( ' #removeTip S '). html (' Delete exception: ' + Data.error + ' please refresh retry. ' ; }}, ' json ');
var express = require (' Express '); var router = Express. Router (); Router.route ('/user/delete '). Post (function(req, res) { var _id = req.body.id; // 123456});
PS: If the post does not pass any parameters to the background, the value of req.body is of course empty object {}
Reference Documentation:
Http://expressjs.com/api.html#request
Express4.x Request Object Obtaining parameter method small talk about "original"