Your location:Tech News>News>Script>Several ways of nodejs get / post request Summary

Latest News

Several ways of nodejs get / post request Summary

Author:fsadmin

Views:

  In the most recent study sent to the front-end server data and requests data, the following sums up the server to send a request in several different ways with get and post requests:

  1.Form method of the form:

  (1) get method

  Front-end code:

  

  

  

  

  

  

  

  

  

  Server code:

  The method of the get json first configuration file, enter the command in command npm-init, and then to express install modules required, also need to create a static resource folder placement (wwwroot) inside the folder, then the code is as follows:

  var express = require ( ''express''); // introduced module

  var web = express (); // create a web application module using

  web.use (express.static ( ''wwwroot'')); // call the static method use method

  web.get ( ''/ login'', function (request, response)

  {

  Use get method parameter 1 2 callback interface parameter (Parameter 1 transmits a request to the server data parameters returned by the server 2)

  var name = request.query.username; // get account sent from the front

  var psw = request.query.password; // get the password sent from the front

  response.status ( ''200'').send ( ''content of the input is'' + name +''
''+ Psw);

  })

  web.listen ( ''8080'', function () // Start the server listens on port 8080

  {

  console.log ( ''startup server'');

  })

  (2) post method

  Distal: method requires a post form inside the change method = GET mthod = POST, post indication method;

  Server: In addition to the get method claim, further body-parser module needs to be introduced, and the encoding of the url;

  var express = require ( ''express'');

  var bodyParser = require ( ''body-parser'');

  var web = express ();

  web.use (express.static ( ''wwwroot''));

  // url uniform resource identifier encoded coding deployment

  web.use (bodyParser.urlencoded ({extended: false}));

  web.post ( ''/ login'', function (request, response)

  {

  var name = request.body.username;

  var psw = request.body.password;

  if (name != ''599115316 @ qq.com ''|| psw != ''123456'')

  {

  response.send ( ''Login failed'')

  }

  else

  {

  response.send ( ''Successful landing'')

  }

  })

  web.listen ( ''8080'', function ()

  {

  console.log ( ''startup server'');

  })

  2.xhr (XML HTTP Request Request Mode There are three ways get / post / formdata)

  XHR is the core ajax using XHR can send data to the server can parse the data returned by the server;

  (1) xhr the get method:

  front end:

  

  

  server:

  First need to install the modules used, then a request module;

  var express = require ( ''expres'');

  var app = express ();

  app.use (express.static ( ''wwwroot''));

  app.get ( ''/ comment'', function (request, response)

  {

  response.Send ( ''has received the get sent evaluation method ");

  })

  app.listen ( ''3000'', function ()

  {

  console.log ( ''startup server'');

  })

  (2) xhr the post method:

  front end:

  

  

  server:

  Module (body-parser module) needs to be introduced post as well as the method used in encoding url;

  var express = require ( ''express'');

  var bodyParser = require ( ''body-parser'');

  var app = express ();

  app.use (express.static ( ''wwwroot''));

  app.use (bodyParser.urlencoded ({extended: false}));

  app.post ( ''/ comment'', function (request, response)

  {

  response.Send ( ''has received the evaluation method of the transmission post'');

  })

  app.listen ( ''3000'', function ()

  {

  console.log ( ''startup server'');

  })

  (3) xhr of formdata Method:

  front end:

  

  

  server:

  var express = require ( ''express'');

  var bodyParser = require ( ''body-parser'');

  var multer = require ( ''multer''); // used to form the form required to use a module

  var formData = multer ();

  var app = express ();

  app.use (express.static ( ''wwwroot''));

  app.use (bodyParser.urlencoded ({extended: false}));

  // If formdata data submitted, it must be used in the parameter array (), array () member which will first parse the data request retransmission of data

  app.post ( ''/ comment'', formData.array (), function (request, response)

  {

  response.Send ( ''has received the evaluation method of the transmission post'');

  })

  app.listen ( ''3000'', function ()

  {

  console.log ( ''startup server'');

  })

  3.ajax request:

  Generally not need to use ajax ajax request using request may obtain error information and some other instructions need to reference jquery using ajax

  (1) ajax''s get:

  front end:

  

  

  server:

  var express = require ( ''express'');

  var app = express ();

  app.use (express.static ( ''wwwroot''));

  app.get ( ''/ login'', function ()

  {

  if (request.query.name == ''Bob'' && request.query.password == ''123456'')

  {

  response.send ( ''Login successful'');

  }

  else

  {

  response.send ( ''Login failed'');

  }

  })

  app.listen ( ''8080'', function ()

  {

  console.log ( ''startup server'');

  })

  (2) ajax''s post:

  front end:

  

  

  server:

  var express = require ( ''express'');

  var bodyParser = require ( ''body-parser'');

  var app = express ();

  app.use (express.static ( ''wwwroot''));

  app.use (bodyParser.urlencoded ({extended: false}));

  app.listen ( ''8080'', function ()

  {

  console.log ( ''startup server'');

  })

  app.post ( ''/ login'', function (request, response)

  {

  if (request.body.name == ''Bob'' && request.body.password == 666)

  {

  response.send ( ''Login successful'');

  }

  else

  {

  response.send ( ''Login failed'');

  }

  })

  (2) ajax of ajax:

  front end:

  

  

  The server can use the code inside the methods of the get and post ajax above, by way ajax request type is set to get a manner mode or post.

  This nodejs above the get / post requests summary of several ways that small series to share the entire contents of everyone, and I hope to give you a reference, I hope you will support script Home.

  You may also be interested in the article: Node.js series initiated get / post request (2) vue basis of use get, post, jsonp exemplary functions interact asp.Use express with examples to explain nodejs GET and post when the net submit data nodejs with http for post or instance (carrying cookie) get request AngularJS send asynchronous Get / Post request method vuejs using axios asynchronous access using JS + form Form Post and Get embodiment get get and post session by value and proven methods angularJS launched $ http.and $ http post.get request and the get-implemented method JS achieve post request code analysis

Recommend article

Relate article