0

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.


Success Criteria: The result In the browser must be equivalent to the result of

  if (userAgent.toLowerCase().includes(myString)) {
    res.redirect('https:/www.some-unregistered-url.com')
  }

But leaving the actual url unchanged.

johann1301s
  • 343
  • 4
  • 19
  • 1
    It really going to depend, not on your code, but on the hosting rules, for example, Heroku.com will send a request timeout if you don’t send anything for 30 seconds… I would maybe just redirect to a page where you could explain why not use a platform or another and what they could do right… no? – balexandre Dec 17 '22 at 20:32

2 Answers2

0

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

c137
  • 181
  • 9
  • Thank you for responding with this, but this is not satisfactory for my part. – johann1301s Dec 17 '22 at 20:32
  • This is still an HTTP 200 and thats not what the question asks… – balexandre Dec 17 '22 at 20:33
  • The browser(s) usually gives a written explanation. This would be left out by what you propose. – johann1301s Dec 17 '22 at 20:33
  • you can return a 200 status or any kind of error, not returning anything will invoke an exception any way in the client, so you could return an empty document or an error, since you didn't want to see anything on the screen this should do the trick – c137 Dec 17 '22 at 20:35
0

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.

Ralle Mc Black
  • 1,065
  • 1
  • 8
  • 16