Home > Enterprise >  Use express to not respond to specific requests
Use express to not respond to specific requests

Time:12-18

I would like to serve some HTML based on a normal browser GET request. But I would like to respond with nothing at all, (as though the domain is not even registered) if the user-agent request header contains the string from the js variable myString.

I think this means that my server must not respond with status 100. But how is this done?

I tried just adding a return statement, which just left the request hanging. I also tried adding res.status(404).end() and res.destroy(), but the result in the browser is different than no response at all.

How can I 'not-respond' to a browser request for a specific user-agent, but otherwise respond normally?

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const userAgent = req.headers['user-agent']
  const myString = 'firefox'
  if (userAgent.toLowerCase().includes(myString)) {
    res.destroy()
    return
  }
  res.setHeader("Content-Type", "text/html")
  res.send('<html><body>hello</body></html>')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

I expected no visible result in the browser at all, as though the domain was not registered.


The reason I am asking is that I plan on not responding to iPhone requests, given that the iOS Safari browser has so many issues regarding dynamic viewport and all the difficulties that follow from poor implementation by apple.

CodePudding user response:

how about returning an empty html document?

res.send('<html><body></body></html>')

you could also try

res.send('')

you should also check out this for server-side browser detection

server-side browser detection? node.js

CodePudding user response:

Try like so: res.set("Connection", "close"); or res.connection.destroy();

but if you really dont want to allow Firefox. You should send a StatusCode 40x. Badrequest 400 or 403 Forbidden, with a message like. Client not allow.

  • Related