Search code examples
javascriptnode.jsexpressget

Express edits URL query to "favicon.ico"


When I send a GET request to my node.js/express web server with a URL after the route, instead of the server recording said URL, it instead stores favicon.ico:

var express = require("express");
var app = express();

app.get("/:query", function (req, res) {
    var query = req.params.query;
    console.log(query);
})

var port = process.env.PORT || 8080;
app.listen(port,  function () {
    console.log('Node.js listening on port ' + port + '...');
});

So, when I go to https://my-domain-url.io/http://www.google.co.uk what gets printed to the console is: favicon.ico instead of http://google.co.uk

Does anyone know why this happens and how to prevent it?

(I am currently using a workaround by using req.headers.referer which gives me the full https://my-domain-url.io/http://www.google.co.uk which I then manually parse.)


Solution

  • One way to do this:

    var express = require("express");
    var app = express();
    
    app.get("/*", function (req, res, next) {
        var query = req.params[0];
        console.log(query);
        next(); //you need add next, otherwise your query will hang there
    })
    
    var port = process.env.PORT || 8080;
    app.listen(port,  function () {
        console.log('Node.js listening on port ' + port + '...');
    });