node.js、express.js - 提供单个静态文件的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11473607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:02:01  来源:igfitidea点击:

node.js, express.js - What is the easiest way to serve a single static file?

node.jsexpress

提问by James Vickers

I have already seen and made use of:

我已经看到并利用了:

app.use("/css", express.static(__dirname + '/css'));

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?

我不希望提供根目录中的所有文件,只提供一个文件“ipad.htm”。使用 express.js 以最少的代码做到这一点的最佳方法是什么?

回答by ebohlman

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).

res.sendfile(path_to_file);是你所需要的全部; 它将自动设置正确的标题并传输文件(它在内部使用与 相同的代码express.static)。

回答by Greg Bacchus

app.use("/css/myfile.css", express.static(__dirname + '/css/myfile.css'));

回答by JoWie

I published a small library for serving single static files: https://www.npmjs.com/package/connect-static-file

我发布了一个用于服务单个静态文件的小型库:https: //www.npmjs.com/package/connect-static-file

npm install connect-static-file

npm install connect-static-file

var staticFile = require('connect-static-file'); app.use('/ipad', staticFile(__dirname + '/ipad.html'));

var staticFile = require('connect-static-file'); app.use('/ipad', staticFile(__dirname + '/ipad.html'));

The main difference with express.static is that it does not care about the request url, it always serves that file if the route matches. It does not suddenly start serving an entire directory if you name your directory "ipad.html".

与 express.static 的主要区别在于它不关心请求 url,如果路由匹配,它总是提供该文件。如果您将目录命名为“ipad.html”,它不会突然开始为整个目录提供服务。

回答by ye olde noobe

If you precisely want to serve a single static file, you can use this syntax (example base on the original question, ipad.htm under root directory):

如果您只想提供单个静态文件,则可以使用以下语法(基于原始问题的示例,根目录下的 ipad.htm):

app.use('/ipad.htm', express.static(__dirname, {index: 'ipad.htm'}));

The default behaviour of express.static(dir) is that it will try to send an 'index.html' file from the required dir. The indexoption overrides this behaviour and lets you pick the intended file instead.

express.static( dir)的默认行为是它会尝试从所需的dir发送一个“index.html”文件。该指数选项覆盖此行为,并让您挑选预期的文件,而不是。

回答by Steji

Quick example building on JoWie's answer:

基于 JoWie 的回答的快速示例:

npm install connect-static-file

npm install 连接静态文件

npm install express

npm 安装快递

var express = require('express');
var staticFile = require('connect-static-file');

var path = 'pathto/myfile.txt';
var options = {};
var uri = '/';
var app = express();
var port = 3000;

app.use(uri, staticFile(path, options));

app.listen(port, () => console.log(`Serving ${path} on localhost:${port}${uri}`));

回答by Pickels

 fs.createReadStream(path).pipe(res);