如何在Express.js中重定向到某个路由after/at向客户端发送响应的时间?
How to redirect to a certain route after/at the time of sending response to the client side, in Express.js?
我是 Express.JS 的初级学习者,在路由重定向方面遇到问题。我只想向用户显示一条消息“您的数据已成功注册。”然后在一段时间后,我想再次将他们重定向到表单页面。但是其中几乎没有错误。
这是我在文本文件中写入数据的代码,然后在显示消息后,我想重定向到 /form 路由。
router.use('/form',(req,res)=>{
console.log("form is loaded ");
res.sendFile(path.join(rootDir,'views','form.html'));
});
router.post('/submit',(req,res)=>{
const data=()=>{
const time=Date();
const firstName=req.body.firstName;
const lastName=req.body.LastName;
const email= req.body.email;
const address=req.body.address;
const state=req.body.state;
const phone= req.body.phone;
const pinCode= req.body.pinCode;
let data=time+"\n"+' Name ---> '+firstName+" "+lastName+'\n';
data+=' E-mail ---> '+email+'\n';
data+=' Phone ---> '+phone+'\n';
data+=' Address ---> '+address+'\n';
data+=' State ---> '+state+' - '+pinCode+'\n\n';
return data;
}
// console.log(firstName,lastName,email,phone,address,state,pinCode);
fs.appendFile(dbFile,data(),(error)=>{
console.log("Data added to database");
});
res.send('Your data is registered successfully.');
setInterval(()=>{res.redirect('/form');}, 3000);
});
在那种情况下我收到以下错误:
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: **Cannot set headers after they are sent to the client**
at ServerResponse.setHeader (_http_outgoing.js:530:11)
at ServerResponse.header (/home/infogirdinformatics/Desktop/Nilesh/IG/Express.js/Day 15/Task 3/node_modules/express/lib/response.js:771:10)
请给我任何方法或解决方案。感谢您的帮助和时间。
您不能在发送响应后发送 HTTP 重定向,所以我可能会这样做:
res.send(`
<html>
<head>
<meta http-equiv="refresh" content="3;url=/form" />
</head>
<body>Your data is registered successfully.</body>
</html>
`);
并使用 HTML Redirect
我是 Express.JS 的初级学习者,在路由重定向方面遇到问题。我只想向用户显示一条消息“您的数据已成功注册。”然后在一段时间后,我想再次将他们重定向到表单页面。但是其中几乎没有错误。
这是我在文本文件中写入数据的代码,然后在显示消息后,我想重定向到 /form 路由。
router.use('/form',(req,res)=>{
console.log("form is loaded ");
res.sendFile(path.join(rootDir,'views','form.html'));
});
router.post('/submit',(req,res)=>{
const data=()=>{
const time=Date();
const firstName=req.body.firstName;
const lastName=req.body.LastName;
const email= req.body.email;
const address=req.body.address;
const state=req.body.state;
const phone= req.body.phone;
const pinCode= req.body.pinCode;
let data=time+"\n"+' Name ---> '+firstName+" "+lastName+'\n';
data+=' E-mail ---> '+email+'\n';
data+=' Phone ---> '+phone+'\n';
data+=' Address ---> '+address+'\n';
data+=' State ---> '+state+' - '+pinCode+'\n\n';
return data;
}
// console.log(firstName,lastName,email,phone,address,state,pinCode);
fs.appendFile(dbFile,data(),(error)=>{
console.log("Data added to database");
});
res.send('Your data is registered successfully.');
setInterval(()=>{res.redirect('/form');}, 3000);
});
在那种情况下我收到以下错误:
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: **Cannot set headers after they are sent to the client**
at ServerResponse.setHeader (_http_outgoing.js:530:11)
at ServerResponse.header (/home/infogirdinformatics/Desktop/Nilesh/IG/Express.js/Day 15/Task 3/node_modules/express/lib/response.js:771:10)
请给我任何方法或解决方案。感谢您的帮助和时间。
您不能在发送响应后发送 HTTP 重定向,所以我可能会这样做:
res.send(`
<html>
<head>
<meta http-equiv="refresh" content="3;url=/form" />
</head>
<body>Your data is registered successfully.</body>
</html>
`);
并使用 HTML Redirect