Skip to content

Commit

Permalink
fix: 404 error for options requests
Browse files Browse the repository at this point in the history
Signed-off-by: Avior <[email protected]>
  • Loading branch information
Aviortheking committed Jan 3, 2024
1 parent 12ed23b commit 28fcb66
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@ const VERSION = 2
// Init Express server
const server = express()

// Set CORS global headers
server.use((_, res, next) => {
res
.setHeader('Access-Control-Allow-Origin', '*')
.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
.setHeader('Access-Control-Allow-Headers', 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range')
.setHeader('Access-Control-Expose-Headers', 'Content-Length,Content-Range')
next()
})

// Route logging / Error logging for debugging
server.use((req, res, next) => {
const now = new Date()
// Date of request User-Agent 32 first chars request Method
let prefix = `\x1b[2m${now.toISOString()}\x1b[22m ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.padEnd(7)}`
let prefix = `\x1b[2m${now.toISOString()}\x1b[22m ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.toUpperCase().padEnd(7)}`

const url = new URL(req.url, `http://${req.headers.host}`)
const fullURL = url.toString()
Expand Down Expand Up @@ -51,11 +41,19 @@ server.use((req, res, next) => {
next()
})

/**
* Handle options requests
*/
server.options('/*', (_, res) => {
res.status(200).send()
// Set CORS global headers
server.use((req, res, next) => {
res
.setHeader('Access-Control-Allow-Origin', '*')
.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
.setHeader('Access-Control-Allow-Headers', 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range')
.setHeader('Access-Control-Expose-Headers', 'Content-Length,Content-Range')

if (req.method.toUpperCase() === 'OPTIONS') {
res.status(200).send('ok')
return
}
next()
})

server.get('/', (_, res) => {
Expand Down

0 comments on commit 28fcb66

Please sign in to comment.