我有一個快速/節點應用程序,通過express/router/like/user等類似的路由器暴露GET終點。響應是一個JSON,我想當我打localhost:8080/api/user時將JSON下載到文件中。在express/node應用程序中保存對一個文件的HTTP GET響應
我試過res.download但不知道如何處理它的響應數據。這可能是一個重複的問題,但我無法找到一個特別針對這個用例的例子。
當在瀏覽器中調用端點時,它應該提示下載,然後應該下載到默認位置。
router.route('/user')
.get((req, res) => {
MyService.get().then((result) => { // the get method resolves a promise with the data
// Prompt for download
}).catch((err) => {
console.log(err);
res.status(500).json({
status: 500,
data: err
});
});
});
謝謝您的答覆。我需要的僅僅是當我點擊/ api /用戶端點 – Sai
時下載對文件的響應。然後我的方法適用於你。 – Ozgur