node.js,express.js - 提供单个静态文件最简单的方法是什么? [英] node.js, express.js - What is the easiest way to serve a single static file?
本文介绍了node.js,express.js - 提供单个静态文件最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到并使用了:
app.use("/css", express.static(__dirname + '/css'));
我不希望从根目录提供所有文件,只有一个文件'ipad。 HTM。使用express.js,最小代码的最佳方式是什么?
I do not wish to serve all files from the root directory, only a single file, 'ipad.htm'. What is the best way to do this with the minimum amount of code, using express.js?
推荐答案
res.sendFile(path_to_file);
是你需要的;它将自动设置正确的标题并传输文件(它在内部使用与 express.static
相同的代码)。
res.sendFile(path_to_file);
is all you need; it will automatically set the correct headers and transfer the file (it internally uses the same code as express.static
).
这篇关于node.js,express.js - 提供单个静态文件最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文