totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

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

Closed luasenvy closed 3 years ago

luasenvy commented 3 years ago

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 :)

petersirka commented 3 years ago

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.

luasenvy commented 3 years ago

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