簡體   English   中英

Node.js,表達:處理后的數據,並將數據發送到api

[英]Node.js, express: Process data in post, and send data to api

我正在構建一個Web應用程序,該應用程序通過用戶輸入獲取數據,在服務器中進行處理,然后將處理后的數據顯示回客戶端。

我的一般工作流程如下:

  1. 用戶在瀏覽器中上傳一些數據

  2. 通過POST方法接收數據(使用Express)

  3. 對接收到的數據運行內部數據管道

  4. 將處理后的數據發送到api

  5. 客戶端將使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM