node.js res.sendFile 绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25463423/
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
res.sendFile absolute path
提问by Kaya Toast
If I do a
如果我做一个
res.sendfile('public/index1.html');
then I get a server console warning
然后我收到服务器控制台警告
express deprecated
res.sendfile: Useres.sendFileinstead
express deprecated
res.sendfile:res.sendFile改用
but it works fine on the client side.
但它在客户端运行良好。
But when I change it to
但是当我把它改成
res.sendFile('public/index1.html');
I get an error
我收到一个错误
TypeError: path must be absolute or specify root to
res.sendFile
类型错误:路径必须是绝对的或指定根为
res.sendFile
and index1.htmlis not rendered.
并且index1.html不呈现。
I am unable to figure out what the absolute path is. I have publicdirectory at the same level as server.js. I am doing the res.sendFilefrom with server.js. I have also declared app.use(express.static(path.join(__dirname, 'public')));
我无法弄清楚绝对路径是什么。我的public目录与server.js. 我正在做res.sendFilefrom with server.js。我也声明了app.use(express.static(path.join(__dirname, 'public')));
Adding my directory structure:
添加我的目录结构:
/Users/sj/test/
....app/
........models/
....public/
........index1.html
What is the absolute path to be specified here ?
这里要指定的绝对路径是什么?
I'm using Express 4.x.
我正在使用 Express 4.x。
回答by Mike S
The express.staticmiddleware is separate from res.sendFile, so initializing it with an absolute path to your publicdirectory won't do anything to res.sendFile. You need to use an absolute path directly with res.sendFile. There are two simple ways to do it:
该express.static中间件是独立的res.sendFile,所以用你的绝对路径初始化它public目录不会做任何事情res.sendFile。您需要直接使用绝对路径res.sendFile。有两种简单的方法可以做到:
res.sendFile(path.join(__dirname, '../public', 'index1.html'));res.sendFile('index1.html', { root: path.join(__dirname, '../public') });
res.sendFile(path.join(__dirname, '../public', 'index1.html'));res.sendFile('index1.html', { root: path.join(__dirname, '../public') });
Note:__dirnamereturns the directory that the currently executing script is in. In your case, it looks like server.jsis in app/. So, to get to public, you'll need back out one level first: ../public/index1.html.
注意:__dirname返回当前正在执行的脚本所在的目录。在您的情况下,它看起来像是server.jsin app/。因此,要到达public,您需要先退出一个级别:../public/index1.html。
Note:pathis a built-in modulethat needs to be required for the above code to work: var path = require('path');
注意:path是一个内置模块,需要required 才能使上述代码工作:var path = require('path');
回答by Kshitij Choudhary
Just try this instead:
试试这个:
res.sendFile('public/index1.html' , { root : __dirname});
This worked for me. the root:__dirname will take the address where server.js is in the above example and then to get to the index1.html ( in this case) the returned path is to get to the directory where public folder is.
这对我有用。root:__dirname 将获取上面示例中 server.js 所在的地址,然后到达 index1.html(在本例中),返回的路径是到达公共文件夹所在的目录。
回答by SOu?aan G?g
res.sendFile( __dirname + "/public/" + "index1.html" );
where __dirnamewill manage the name of the directory that the currently executing script ( server.js) resides in.
where__dirname将管理当前正在执行的脚本 ( server.js) 所在的目录的名称。
回答by Phil Gibbins
An alternative that hasn't been listed yet that worked for me is simply using path.resolvewith either separate strings or just one with the whole path:
尚未列出但对我有用的替代方法是简单地使用path.resolve单独的字符串或仅使用整个路径的字符串:
// comma separated
app.get('/', function(req, res) {
res.sendFile( path.resolve('src', 'app', 'index.html') );
});
Or
或者
// just one string with the path
app.get('/', function(req, res) {
res.sendFile( path.resolve('src/app/index.html') );
});
(Node v6.10.0)
(节点 v6.10.0)
Idea sourced from https://stackoverflow.com/a/14594282/6189078
回答by Jaime Gómez
Based on the other answers, this is a simple example of how to accomplish the most common requirement:
根据其他答案,这是一个如何完成最常见要求的简单示例:
const app = express()
app.use(express.static('public')) // relative path of client-side code
app.get('*', function(req, res) {
res.sendFile('index.html', { root: __dirname })
})
app.listen(process.env.PORT)
This also doubles as a simple way to respond with index.html on every request, because I'm using a star *to catch all files that weren't found in your static (public) directory; which is the most common use case for web-apps. Change to /to return the index only in the root path.
这也是一种在每个请求上使用 index.html 响应的简单方法,因为我使用星号*来捕获在您的静态(公共)目录中找不到的所有文件;这是网络应用程序最常见的用例。更改为/仅返回根路径中的索引。
回答by boms
I tried this and it worked.
我试过了,它奏效了。
app.get('/', function (req, res) {
res.sendFile('public/index.html', { root: __dirname });
});
回答by Abdennour TOUMI
process.cwd()returns the absolute path of your project.
process.cwd()返回项目的绝对路径。
Then :
然后 :
res.sendFile( `${process.cwd()}/public/index1.html` );
回答by Tim Anishere
Another way to do this by writing less code.
另一种方法是通过编写更少的代码来做到这一点。
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendFile('index.html');
});
回答by Aref.T
you can use send instead of sendFile so you wont face with error! this works will help you!
您可以使用 send 而不是 sendFile 这样您就不会遇到错误!这个作品会帮助你!
fs.readFile('public/index1.html',(err,data)=>{
if(err){
consol.log(err);
}else {
res.setHeader('Content-Type', 'application/pdf');
for telling browser that your response is type of PDF
用于告诉浏览器您的响应是 PDF 类型
res.setHeader('Content-Disposition', 'attachment; filename='your_file_name_for_client.pdf');
if you want that file open immediately on the same page after user download it.write 'inline' instead attachment in above code.
如果您希望在用户下载后立即在同一页面上打开该文件。请在上面的代码中写入“内联”而不是附件。
res.send(data)
回答by Hymanson D
I use Node.Js and had the same problem... I solved just adding a '/' in the beggining of every script and link to an css static file.
我使用 Node.Js 并遇到了同样的问题......我解决了在每个脚本的开头添加一个“/”并链接到一个 css 静态文件的问题。
Before:
前:
<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">
After:
后:
<link rel="stylesheet" href="/assets/css/bootstrap/bootstrap.min.css">

