我想模塊化我的代碼,並利用從快速路由器到包含我的控制器邏輯的外部模塊的函數調用。在處理請求後,如何將路由變量傳遞迴路由器?快遞路由器和控制器邏輯之間傳遞變量
const express = require('express') ;
const router = express.Router();
const bodyParser = require('body-parser')
const requestSomething = require('./controller/abc.js');
router.post('/', function (req,res, next){
requestSomething() <----// Need Variable from this
next()
},function(req,res,next){
sendOrder(X) <--------//So I can use it further along in chain
}
);
module.exports = router;
//Controller logic, how to send body back to router??
const requestSomething = (req,res,next)=>{
let options = { .... };
requestSomething(options, function (error, response, body) {
if (error) throw new Error(error);
let x = JSON.parse(body); <--- How do I pass this back to router?
})
}
}
謝謝你,因爲我一直在嘗試響應讓它工作,但我得到一個錯誤。無法讀取未定義的屬性'then',請requestSomething()。then。函數調用確實發生。 –
requestSomething()是API調用嗎? –
是的,它是一個API調用。 –