skip to Main Content

Trying to create a rest api that processes excel data. I want to let the client know that the files have been received.

app.post('/upload',upload.fields([{name: 'raw'},{name: 'report'}]), (req: any, res: any)=>{
    
    res.send(statuscode 200)

    // do async excel operations

    return res.json({data: data});
})

I don’t want to send a response after the client has been waiting for 30 seconds


await excelOperations();
return res.json()

2

Answers


  1. With the traditional HTTP request-response model, you don’t. You send the acknowledgement as the response to the data submission, when the upload has completed. Then the client makes another request (or even multiple ones) to inquire about the status of the processing, which you respond to with a progress report response each, and finally the client makes another request to download the results, to which you respond with the processed resource.

    You could however use server-sent events or websockets which would allow you to send multiple things as long as the connection is open.

    Login or Signup to reply.
  2. To complete Bergi answer you can do something like that :

    • Create a joblist object
    • When hitting the /upload endpoint, before uploading create a new job id with a status "pending" and return the job id as response
    • When upload complete (after your async code) change the status of the job to completed
    • Create a new endpoint [GET] /job/:jobId that return status of a job stored in your job object
    • On the front side check in interval the endpoint /job/:jobId with the job id returned by [POST] / upload

    This code is just an example to explain the answer, adapt it to your need, handle error etc

    Code example

    
    const NanoidAsync = require('nanoid/async');
    let jobList = {}
    
    app.post('/upload',upload.fields([{name: 'raw'},{name: 'report'}]), (req: any, res: any)=>{
        
        let jobId = await NanoidAsync.nanoid();
        jobList[jobId] = {status:"pending"}
        res.send(jobList[jobId])
        await excelOperations();
    
    })
    
    app.get('/job/:jobId', (req: any, res: any)=>{
        res.send(jobList[jobId])
    })
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search