Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To retrieve the legitimate source hostname in Nodejs Express, you can use the req.get('host') method, which returns the value of the Host header in the HTTP request.

Here's an example code snippet:

app.get('/example', function(req, res) {
  var clientHost = req.get('host');
  var serverHost = req.hostname;

  if (clientHost === serverHost) {
    // Request is legitimate
    res.send('Legitimate request');
  } else {
    // Spoofed request
    res.send('Spoofed request');
  }
});

In this example, we retrieve the client's hostname using the req.get('host') method and compare it with the server's hostname using the req.hostname method. If the two values match, the request is considered legitimate, and we can proceed with the requested action. If the values do not match, the request is considered spoofed, and we can reject or handle it accordingly.