노드

2016-07-02 2 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);