NodeJS 在Express中根据查询参数调用路由处理程序[已关闭]

omvjsjqw  于 5个月前  发布在  Node.js
关注(0)|答案(1)|浏览(85)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
昨天就关门了。
Improve this question
我正在使用的外部API通过将用户发送到我们的应用程序的根目录(带有hmac和其他查询参数)来启动OAuth2.0进程(注意,这是在授予用户授权时收到回调之前)。因此,如果没有hmac参数,我希望服务于我的静态站点,如果是的话,调用一个处理函数。如果我把所有的验证代码都嵌入到我的应用程序的“else”语句中,那么我的验证代码就可以正常工作了。get('/')但是我更喜欢清理它,然后把控制权交给一个单独的处理函数。
下面的示例代码显示“TypeError res.redirect不是一个函数”。

const morgan = require('morgan');

const app = express();
app.use(morgan('dev'));

app.get('/', (req, res, next) => {
    if (!req.query.hmac) {
        console.log('No special params. Serving static.');
        next();
    } else {
        console.log('handling hmac parameter');
        handleHmac(res, req);
    }
})

app.use(express.static('public'));

const handleHmac = (req, res) => {
    //validate hmac and ultimatley redirect to external auth site
    console.log('handleHmac function called')   //successfully prints in test
    res.redirect('/mockexternal.html')  //TypeError: res.redirect is not a function
}

app.listen(3000, () => {
    console.log('Listening on Port 3000')
})```

字符串

sy5wg1nm

sy5wg1nm1#

将函数调用handleHmac(res, req);更改为handleHmac(req, res);函数参数必须按照正确的顺序。
我建议传递第三个参数next,这样处理程序就可以自由地做任何普通处理程序可能做的事情,而不需要更改调用代码。

handleHmac(req, res, next);

字符串

相关问题