2016-07-02 4 views
0

のExpress.JSでExpressがUTF8文字を読み取ると、Expressはこの要求パス-/wiąz.txt/wiÄz.txtと読み込み、/wiÄz.txt doesn't existとなりますが、wiąz.txtが存在します。リクエストパスにutf8文字を読み込むことは可能ですか?ノード

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'); 
}); 
+0

あなたのコードを問題に投稿できますか? –

+0

完了。それをもう一度見てください。 –

+0

'fs.readFileSync( '+' decodeURIComponent(req.path)+ '。txt'、 'utf8')' –

答えて

1

これを試してください。

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