totaljs/framework

has the way changed to set response status code in workflow?

Closed this issue · 2 comments

i use controller -> schema.workflow process.

and i work in progress implementing range partial content.

code is like below:

schema.workflow('send-file', async function($) {
  if ( $.controller.req.headers.range ) 
    $.controller.status = 206
  else
    $.controller.status = 200
  
  const readableStream = fs.createReadStream(filepath,{ start, range })
  readableStream.on('open', () => readableStream.pipe($.controller.res))
})

but response always setted 200.

so, i change the code:

schema.workflow('send-file', async function($) {
  if ( $.controller.req.headers.range )
    $.controller.status = $.controller.res.statusCode = 206
  else
    $.controller.status = $.controller.res.statusCode = 200
  
  const readableStream = fs.createReadStream(filepath,{ start, range })
  readableStream.on('open', () => readableStream.pipe($.controller.res))
})

i'm not sure this code not have a side effect for total.js but, well worked.

if has a better solution, please guide me.

feel free close this issue

thanks :)

It's better use:

schema.workflow('send-file', function($) {
     $.controller.file('~' + filepath);
     $.cancel();
})

Your code doesn't handle headers.range correcly, but Total.js .file() solves everything.

oh, thank you for reply i will use your guide! more simple 👍