Szymon Marczak
Szymon Marczak

Reputation: 1093

Read UTF8 chars in req.path using Express.JS in Node

Express reads this request path - /wiąz.txt as /wiÄz.txt, and I get /wiÄz.txt doesn't exist, but wiąz.txt exists. Is it possible to read utf8 chars in request path?

var express = require('express');
var fs = require('fs');
var app = express();
app.set('etag', false);
app.set('x-powered-by', false);

app.route('*').all(function(req, res) {
    res.set('Content-Type', 'text/plain');

    try {
        var file = fs.readFileSync('.' + req.path, 'utf8'); // req.path starts always with /, the result is ./FILE
        res.send(file);
    } catch (e) {
        res.send(req.path + " doesn't exist");
    }
});

app.listen(80, function () {
    console.log('HTTP Server is now running on port 80');
});

Upvotes: 0

Views: 344

Answers (1)

Lalit Goswami
Lalit Goswami

Reputation: 816

Try this.

var file = fs.readFileSync('.' + decodeURIComponent(req.path), 'utf8');
res.send(file);

Upvotes: 1

Related Questions