Skip to content

Commit

Permalink
fix(server): set content-type before sending response
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddarth committed Jul 27, 2020
1 parent 93df8bc commit 4f20708
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export class AuthlessServer {
}

let responseFormat: URLParams['responseFormat'] = 'json'
if(urlParams.responseFormat === 'png') {
responseFormat = urlParams.responseFormat
if(urlParams?.responseFormat === 'png') {
responseFormat = urlParams?.responseFormat
}
// let service handle the page
const authlessResponse = await selectedDomainPath.pageHandler(
Expand All @@ -165,22 +165,25 @@ export class AuthlessServer {
)

if (responseFormat === 'json') {
expressResponse.set('Content-Type', 'application/json; charset=utf-8')
return expressResponse
expressResponse
.status(200)
.set('Content-Type', 'application/json; charset=utf-8')
.send({
meta: authlessResponse.meta,
page: authlessResponse.page,
main: authlessResponse.main,
xhrs: authlessResponse.xhrs,
})
.set('Content-Type', 'text/html')
.end()
}
expressResponse.set('Content-Type', 'text/html')
if (urlParams?.responseFormat === 'png') {
} else if (responseFormat === 'png') {
expressResponse
.status(200)
.set('Content-Type', 'image/png')
.end(await page.screenshot({fullPage: true}), 'binary')
} else {
expressResponse
.status(501)
.end('Can only handle responseFormat of type json or png')
}
await page.close()
}
Expand Down

0 comments on commit 4f20708

Please sign in to comment.