sCHween
sCHween

Reputation: 123

nodejs with express and request how to support utf8

first of all I have to note that I'm new to all this nodejs stuff. Maybe the question is not worth to use any bytes - but lets see.

I'm trying to fetch some data on a site. See here my code snippet to do that:

app.get('/scrape', function (req, res) {

request({
    uri: 'http://www.admin.ch/index.php',
}, function (err, response, body) {
    var self = this;
    self.items = new Array(); 

    if (err && response.statusCode !== 200) {
        console.log('Request error.');
    }
    //jsdom please attach jQuery in the scripts
    jsdom.env({
        html: body,
        scripts: ['http://code.jquery.com/jquery-2.1.1.min.js'],
        done: function(errors, window) {
        var $ = window.jQuery;
        $body = $('body'),
        $threads = $body.find('a:not([href$=\'958206\'])');
        $threads.each(function (i, item) {
             self.items[i] = {
                href:  $(item).attr('href'),
                title: $(item).text().trim(),
                urlObj: url.parse($(item).attr('href'), true) 
            };
        });

    //render a view
        res.render('list', {
            layout: 'layout.jade',
            title: 'Admin YourSelf',
            items: self.items
        });
      }
    });
});

});

So far everything is working as it should. The only thing is that i can't get the right encoding for the data.

Du h�ttest dort nen Stammplatz auf immer (o.T.)

shoulb be

Du hättest dort nen Stammplatz auf immer (o.T.)

any idea how this issue could be solved?

Thanks in advance and for the fish, sCHween

Upvotes: 0

Views: 323

Answers (1)

Edgar
Edgar

Reputation: 113

You may use iconv-lite to convert from ISO-8859-1 :

var request = require("request");
var iconv  = require('iconv-lite');

request({
encoding: null,
uri: 'http://www.admin.ch/index.php',
}, function (err, response, body) {
      var Utf8String = iconv.decode(new Buffer(body), "ISO-8859-1");
});

Upvotes: 1

Related Questions