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:
function()
{
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{console.log(xhr.responseText)} // 服务器接收到数据后返回的数据
}
xhr.open(''/get'',''/comment?custom=小明&score=2&comment=商品质量一般,2分是给快递小哥的'');
xhr.send();
// xhr.open(); 里面有三个参数 ,参数1:设置xhr请求服务器的时候,请求的方式;参数2:设置请求的路径和参数;(?是路径和参数的分割线);参数3:设置同步请求还是异步请求,不写的话默认为异步请求;
}
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:
function post()
{
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
console.log(''接收到服务器返回的信息'' + xhr.responseText);
}
}
xhr.open(''post'',''/comment''); // post方法请求的参数不写在open里面,写在send里面,而且需要设置请求头;
xhr.setRequestHeader(''Content-Type'',''application/x-www-form-urlencoded'');
xhr.send(''custom=小明&score=3&comment=商品还好,快递也及时,但是就想给3分'');
}
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:
function formdata()
{
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
console.log(''formdata方法返回的数据是:'' + xhr.responseText);
}
}
xhr.open(''post'',''/comment'');
var form=new FormData();
form.append(''custom'',''小明'');
form.append(''score'',''5'');
form.append(''comment'',''看你那么辛苦,给你5分好了'');
xhr.send(form);
}
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:
$(''#get'').click(function()
{
$.get(''/login'',{name:''小明'',password:''123456''},function(data,status,xhr)
{
console.log(''服务器返回的信息是'' + data);
})
// $.get() 发起一个get请求,参数1:请求的接口;参数2:传递给服务器的数据对象;参数3:回调函数(参数1:服务器返回的数据;参数2:状态;参数3:xhr对象");
})
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:
$(''#post'').click(function()
{
$.post(''/login'',{name:''小明'',password:''666''},function(data,status,xhr)
{
console.log(''服务器返回的数据:'' + data)
})
})
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:
$(''#id'').click(function()
{
// $.ajax() 发起ajax请求;
$.ajax({
url :''/login'', // 请求的接口地址
type:''post'', // 请求的方式,默认为get请求
data:{name:''小明'',password:''123''}, // 发送到服务器的数据
timeout:10000, // 超时 (10s)
cache:true, // 缓存 默认为true
async:true, // 是否异步
// 同步任务(sync) :当上一个任务没有完成的时候,下一个任务无法开启,有可能会卡死主线程;
//异步任务(Async):当上一个任务没有完成的时候,下一个任务仍然会被执行,用户体验性好;
success:function(data,status,xhr)
{
console.log(''服务器返回的数据是:'' + data);
console.log(''返回的信息是:'' + xhr.getAllResponseHeaders());
}
error:function(xhr,status,error)
{
console.debug(''错误信息:'' + error);
}
complete:function(xhr,status)
{
console.log(''全部流程结束'');
}
})
})
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