Why cant send given POST data as response? When write response.write('Data is: ', data.toString()); i expect see data from post in browser, but its not work, why? if i use response.write('Some string'); not in callback request.on('data',...) i see written data in browser. If i use console.log(data) in callback request.on('data',...) i see data in console. But response not given any data in that callback. Why?
为什么不能发送给定的POST数据作为响应?写response.write('Data is:',data.toString());我希望在浏览器中看到来自帖子的数据,但它不起作用,为什么?如果我使用response.write('Some string');不在回调request.on('data',...)我在浏览器中看到书面数据。如果我在回调request.on('data',...)中使用console.log(data),我会在控制台中看到数据。但是响应没有给出回调中的任何数据。为什么?
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
//console.log(data.toString());//it`s work, i see data in console
response.write('Data is: '+ data.toString()); //but that code is not working. that data not append to response
});
response.write(template);
response.end();
});
3 个解决方案
#1
Fix for your problem
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
var postData = '';
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
postData += data.toString(); // store everything sent with the POST request
});
request.on('close', function(){
response.write(template); // print out the template
if(postData != '')
response.write('Data is: ', postData); // if POST data was sent output the data
response.end(); // close the connection
});
});`
Explanation
Your problem is that node.js/javascript is an event-driven language based on callbacks. When a new request comes in everything in your request
-event-function will be executed. This means that the following will be executed:
你的问题是node.js / javascript是一种基于回调的事件驱动语言。当请求中的所有内容都出现时,将执行事件 - 函数。这意味着将执行以下操作:
-
response.writeHead
which simply predefines the status etc. - definition of a callback for the
data
-event ofrequest
. This means that whenever some POST data gets into the connection the callback will be executed. -
But then: You write your template to the client and then you close the connection. By that every further callback of the
data
-event ofrequest
will write your POST data to a closed connection.
response.writeHead只是预定义状态等。
定义请求的数据事件的回调。这意味着只要某些POST数据进入连接,就会执行回调。
但是:您将模板写入客户端,然后关闭连接。通过这种方式,请求的数据事件的每次进一步回调都会将您的POST数据写入已关闭的连接。
As the result nothing will be displayed at your website.
因此,您的网站上不会显示任何内容。
#2
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
response.write('OK');//thats not as response part
console.log(123);//see that in console
});
response.write(template);
response.end();
});
#3
Thansk for all. I solved my problem. That code works for me
Thansk为所有人。我解决了我的问题。该代码适用于我
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
response.write(data.toString());
});
response.write(template);
request.on('end', function(){
response.end();
});
});
#1
Fix for your problem
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
var postData = '';
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
postData += data.toString(); // store everything sent with the POST request
});
request.on('close', function(){
response.write(template); // print out the template
if(postData != '')
response.write('Data is: ', postData); // if POST data was sent output the data
response.end(); // close the connection
});
});`
Explanation
Your problem is that node.js/javascript is an event-driven language based on callbacks. When a new request comes in everything in your request
-event-function will be executed. This means that the following will be executed:
你的问题是node.js / javascript是一种基于回调的事件驱动语言。当请求中的所有内容都出现时,将执行事件 - 函数。这意味着将执行以下操作:
-
response.writeHead
which simply predefines the status etc. - definition of a callback for the
data
-event ofrequest
. This means that whenever some POST data gets into the connection the callback will be executed. -
But then: You write your template to the client and then you close the connection. By that every further callback of the
data
-event ofrequest
will write your POST data to a closed connection.
response.writeHead只是预定义状态等。
定义请求的数据事件的回调。这意味着只要某些POST数据进入连接,就会执行回调。
但是:您将模板写入客户端,然后关闭连接。通过这种方式,请求的数据事件的每次进一步回调都会将您的POST数据写入已关闭的连接。
As the result nothing will be displayed at your website.
因此,您的网站上不会显示任何内容。
#2
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
response.write('OK');//thats not as response part
console.log(123);//see that in console
});
response.write(template);
response.end();
});
#3
Thansk for all. I solved my problem. That code works for me
Thansk为所有人。我解决了我的问题。该代码适用于我
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';
server.on('request', function(request, response){
response.writeHead(200,{
'Content-Type': 'text/html;charset=utf-8'
});
request.on('data', function(data){
response.write(data.toString());
});
response.write(template);
request.on('end', function(){
response.end();
});
});