Javascript 我的请求on(';end';)未触发。如何解决这个问题?
我刚刚开始学习Node.js,我正在尝试打印通过表单提交的输入值。Javascript 我的请求on(';end';)未触发。如何解决这个问题?,javascript,node.js,http,request,Javascript,Node.js,Http,Request,我刚刚开始学习Node.js,我正在尝试打印通过表单提交的输入值。req.on('data')总是启动,但是req.on('end')从不启动。 有人能告诉我怎么了吗 我的全部代码可以在下面找到 const http = require('http'); const fs = require('fs'); const server = http.createServer((req, res) => { const url = req.url; const method =
req.on('data')
总是启动,但是req.on('end')
从不启动。
有人能告诉我怎么了吗
我的全部代码可以在下面找到
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') {
res.write('<html>');
res.write('<head><title>Enter Message</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button type="submit">Send</button></form></body>'
);
res.write('</html>');
return res.end();
}
if (url === '/message' && method === 'POST') {
const body = [];
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk);
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
});
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
}
// process.exit();
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Sever!</h1></body>');
res.write('</html>');
res.end();
});
server.listen(4000);
然后我必须删除if语句之后的res.*
调用:
//res.setHeader('Content-Type', 'text/html');
//res.write('<html>');
//res.write('<head><title>My First Page</title></head>');
//res.write('<body><h1>Hello from my Node.js Sever!</h1></body>');
//res.write('</html>');
//res.end();
//res.setHeader('Content-Type','text/html');
//res.write(“”);
//res.write(“我的第一页”);
//res.write('Hello from my Node.js Sever!');
//res.write(“”);
//res.end();
现在整个代码如下所示:
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') {
res.write('<html>');
res.write('<head><title>Enter Message</title></head>');
res.write(
'<body><form action="/message" method="POST"><input type="text" name="message"><button type="submit">Send</button></form></body>'
);
res.write('</html>');
return res.end();
}
if (url === '/message' && method === 'POST') {
const body = [];
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk);
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
});
}
// process.exit();
//res.setHeader('Content-Type', 'text/html');
//res.write('<html>');
//res.write('<head><title>My First Page</title></head>');
//res.write('<body><h1>Hello from my Node.js Sever!</h1></body>');
//res.write('</html>');
//res.end();
});
server.listen(4000);
consthttp=require('http');
常数fs=要求('fs');
const server=http.createServer((req,res)=>{
const url=req.url;
const method=req.method;
如果(url=='/')){
res.write(“”);
res.write(“输入消息”);
res.write(
“发送”
);
res.write(“”);
返回res.end();
}
如果(url=='/message'&&method==='POST'){
常量体=[];
请求on('数据',(块)=>{
console.log(块);
推(块);
});
请求开启('end',()=>{
const parsedBody=Buffer.concat(body.toString();
log(parsedBody);
res.statusCode=302;
res.setHeader('Location','/');
返回res.end();
});
}
//process.exit();
//res.setHeader('Content-Type','text/html');
//res.write(“”);
//res.write(“我的第一页”);
//res.write('Hello from my Node.js Sever!');
//res.write(“”);
//res.end();
});
服务器监听(4000);
如果您能深入了解这一问题的原因,我们将不胜感激。Edit
最初的答案仍然适用,但我遗漏了一件事:if块必须返回原始post中的值,否则将运行之后的响应操作,这将再次关闭req/res流
在事件处理程序上返回或不返回没有区别。它不会影响执行,并且返回的值不会在任何地方使用
因此,对于修改后的修复:
if (url === '/message' && method === 'POST') {
// ...
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
res.statusCode = 302;
res.setHeader('Location', '/');
res.end();
});
return;
原始答案
我不是节点http模块方面的专家,所以如果有人愿意填写,请填写。也就是说:
两秒钟意味着基础连接(实际套接字)超时,对于其他请求,该套接字可能保持活动状态。这是正常的
我想这里发生的事情是,由于您基本上要求在耗尽请求流之前关闭响应流,因此即使有更多可用数据,请求流也将被关闭。请注意,实际的底层连接是双向的,结束响应传输意味着您对传入消息不再感兴趣
要解决此问题,请将res.*
调用移动到'end'
事件处理程序:
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
res.statusCode = 302;
res.setHeader('Location', '/');
res.end();
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
res.statusCode = 302;
res.setHeader('Location', '/');
res.end();
});
return req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody);
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
});
经过一点研究,我发现了问题所在。('end')上的
req.on
事件处理程序刚刚注册,但没有被激发,因为它是异步的。因此,它将转到下一个代码块,即:
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title></head>');
res.write('<body><h1>Hello from my Node.js Sever!</h1></body>');
res.write('</html>');
res.end();
这将阻止在
if(url=='/message'&&method==='POST')
语句之后执行req.*.
语句块。是否可以检查`const parsedBody=Buffer.concat(body.toString();`行没有抛出任何错误?基本上添加控制台语句first@AshishModi我试过了,console语句没有运行客户端是否有可能终止连接?您正在立即响应请求。尝试侦听'aborted'
和'close'
事件。@SamiHult我做了req.on('close',(err)=>{console.log(err);})代码>它已打印undefined@SamiHult然后我尝试了req.connection.on('close',(err)=>{console.log(err);})代码>并等待大约2秒,然后返回false。这是什么意思?我刚刚试过,现在流不会关闭,页面也不会重定向回/
,因为res.on('end')
仍然不会被触发。我觉得我的节点可能有问题,或者是一些意想不到的问题,应该表明并不是所有的数据都被消耗了。哪个节点版本?流是否发出error
事件?同时,收到的请求是否有效?它是如何生成的,您可以共享整个HTTP消息吗?好的,我刚刚启动了node,所以我不知道您在说什么,但不,我不认为流会发出错误
事件。我的节点版本是v12.8.1,我刚刚修改了这个问题。我能够启动req.on('end')
,但我需要一个解释,解释它为什么有效。我修改了我的答案-我在修复时忽略了这一点,执行将继续到“我的第一页”块,这是不应该的。代码之所以有效,是因为从末尾删除了res.end()
。我想你应该在if
中返回;无论如何,您可能希望在那里进行默认处理。