일반 파일 형식은 무엇입니까?
설정 MIME 형식 사용 (docs) :
app.get('/file/:name/file.cfg', function (req, res) {
res.set('content-type', 'text/plain');
res.send(<the file file.cfg piped through some sed command involving req.params.name>)
});
당신이 기반 MIME 형식을 설정하는 node-mime
디스크에서 파일 res.sendfile를 사용을 보내려면 파일의 MIME 타입의 사용을 감지하려면 내선 번호
res.sendfile (경로, [options], [fn]]
지정된 경로에서 파일을 전송합니다.
파일 이름의 확장명을 기반으로 Content-Type 응답 헤더 필드의 기본값이 자동으로 설정됩니다. 콜백 fn (err)은 전송이 완료되거나 오류가 발생할 때 호출됩니다.
app.get('/file/:name/file.cfg', function (req, res) {
var path = './storage/' + req.params.name + '.cfg';
if (!fs.existsSync(path)) res.status(404).send('Not found');
else res.sendfile(path);
});
또한 res.download 사용하여 파일을 다운로드 할 브라우저를 강제 할 수 있습니다. express는 훨씬 더 많은 것을 제공해야하며, 문서를 살펴보십시오.
내가 함께 일하게 일어나는 CFG 파일이
app.get('/file/:name/file.cfg', function (req, res) {
fs.readFile('../dir/file.cfg', function(err, data) {
if (err) {
res.send(404);
} else {
res.contentType('text/cfg'); // Or some other more appropriate value
transform(data); // use imagination please, replace with custom code
res.send(data)
}
});
});
(이 노드 REPL의 덤프입니다) :
> express.static.mime.lookup("../kickstart/ks.cfg")
'application/octet-stream'
>
아주 일반적인
출처
2013-07-13 21:58:32
cr0