cio*_*Pep 6 redirect node.js express
我正在使用expressjs,我想做这样的事情:
app.post('/bla',function(req,res,next){
//some code
if(cond){
req.forward('staticFile.html');
}
});
Run Code Online (Sandbox Code Playgroud)
正如Vadim指出的那样,您可以使用res.redirect将重定向发送到客户端.
如果要在不返回客户端的情况下返回静态文件(如建议的注释),则只需在使用__dirname构造后调用sendfile即可.您可以将下面的代码分解为单独的服务器重定向方法.您也可能想要注销路径以确保它符合您的期望.
filePath = __dirname + '/public/' + /* path to file here */;
if (path.existsSync(filePath))
{
res.sendfile(filePath);
}
else
{
res.statusCode = 404;
res.write('404 sorry not found');
res.end();
}
Run Code Online (Sandbox Code Playgroud)
这是参考文档:http://expressjs.com/api.html#res.sendfile
| 归档时间: |
|
| 查看次数: |
15004 次 |
| 最近记录: |