第一分別定義/test
處理功能。
那麼你有兩個選擇。
// ========(1)====== testHandler as another function =============
// you can call this function where ever you want.
var testHandler = function(req, res){
//do something
}
app.get('/test', testHandler);
app.get('/check', function(req, res){
// you can call testHandler function here
testHandler(req, res);
});
// ========(2)======= testHandler as a middleware =================
// if you want to call testHandler before running check handler function.
//
var testHandler = function(req, res, next){
//do something
...
next();
}
app.get('/test', testHandler, function(req, res){});
app.get('/check', testHandler, function(req, res){
// you can call testHandler function here
testHandler(req, res);
});
「從另一個API調用API」的快速方法是在Express服務器內部發送HTTP請求,瀏覽器永遠不會知道內部HTTP調用發生,而不會提及頁面重定向。這對你來說是個好主意嗎?如果是,我很樂意提供一些例子。 – shaochuancs
@shaochuancs請隨時給我一些例子:)我非常感謝 –
不客氣。我已添加了我的答案,請檢查。 – shaochuancs