javascriptnode.jssearch-engineexpress

expressjs node.js serve different data to google/etc bot and human traffic


I want to determine if incoming requests are from a bot (eg google, bing), or a human, and serve different data to each, for example, json data for client javascript to construct the site or preprocessed html.

Using expressjs, is there an easy way to do this? Thanks.


Solution

  • I recommend you to response according to the requested MIME type (which is present in the "Accept" header). You can do this with Express this way:

    app.get('/route', function (req, res) {
        if (req.is('json')) res.json(data);
        else if (req.is('html')) res.render('view', {});
        else ...
    });