[英]Node.js, express: Process data in post, and send data to api
我正在構建一個Web應用程序,該應用程序通過用戶輸入獲取數據,在服務器中進行處理,然后將處理后的數據顯示回客戶端。
我的一般工作流程如下:
用戶在瀏覽器中上傳一些數據
通過POST方法接收數據(使用Express)
對接收到的數據運行內部數據管道
將處理后的數據發送到api
客戶端將使用GET請求訪問api
目前,我在第4步遇到問題。
我試圖像這樣使用嵌套路由器,
const express = require('express');
app = express();
// Simiulate data receiving from client
var getData = function() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data received!")
}, 1000)
})
}
// Simulated data pipeline
var runPipeline = function() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Some data")
}, 1500)
})
}
var sendDataToAPI = function (data) {
// send data to "/api/data"
console.log(data)
}
// get form data from client
// This was done using axios with React
app.post('/upload', (req, res) => {
// Get data
// Process
// Send it to API
getData()
.then(_ => runPipeline())
.then(data => sendDataToAPI)
})
但這並沒有真正的作用...
任何關於替代方法的幫助或建議,將不勝感激。
app.post('/upload', async (req, res) => {
// those block should be in another promise or await
await GetData()
await Process()
// end it to API
getData()
.then(async _ => await runPipeline())
.then(data => sendDataToAPI)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.