res.sendFile绝对路径

时间:2014-08-23 15:20:19

标签: node.js express path

如果我做了

res.sendfile('public/index1.html'); 

然后我收到服务器控制台警告

  

表示已弃用res.sendfile:请改为使用res.sendFile

但它在客户端工作正常。

但是当我把它改成

res.sendFile('public/index1.html');

我收到错误

  

TypeError:path必须是绝对路径或指定root到res.sendFile

index1.html未呈现。

我无法弄清楚绝对路径是什么。我的public目录与server.js处于同一级别。我正在使用res.sendFile进行server.js。我还宣布了app.use(express.static(path.join(__dirname, 'public')));

添加我的目录结构:

/Users/sj/test/
....app/
........models/
....public/
........index1.html

这里指定的绝对路径是什么?

我正在使用Express 4.x。

11 个答案:

答案 0 :(得分:261)

express.static中间件与res.sendFile分开,因此使用public目录的绝对路径对其进行初始化不会对res.sendFile执行任何操作。您需要直接使用res.sendFile的绝对路径。有两种简单的方法可以做到:

  1. res.sendFile(path.join(__dirname, '../public', 'index1.html'));
  2. res.sendFile('index1.html', { root: path.join(__dirname, '../public') });
  3. 注意: __dirname会返回当前正在执行的脚本所在的目录。在您的情况下,server.js似乎在app/中。因此,要获得public,您需要先退出一个级别:../public/index1.html

    注意: path is a built-in module需要require d才能使上述代码生效:var path = require('path');

答案 1 :(得分:41)

试试这个:

res.sendFile('public/index1.html' , { root : __dirname});

这对我有用。 root:__ dirname将获取server.js在上例中的地址,然后转到index1.html(在本例中)返回的路径是到达公用文件夹所在的目录。

答案 2 :(得分:8)

res.sendFile( __dirname + "/public/" + "index1.html" );

其中__dirname将管理当前正在执行的脚本(server.js)所在目录的名称。

答案 3 :(得分:5)

尚未列出的对我有用的替代方法只是将path.resolve与单独的字符串一起使用,或者仅使用整个路径中的一个字符串:

// comma separated
app.get('/', function(req, res) {
    res.sendFile( path.resolve('src', 'app', 'index.html') );
});

或者

// just one string with the path
app.get('/', function(req, res) {
    res.sendFile( path.resolve('src/app/index.html') );
});

(Node v6.10.0)

来自https://stackoverflow.com/a/14594282/6189078

的想法

答案 4 :(得分:4)

我试过这个并且有效。

app.get('/', function (req, res) {
    res.sendFile('public/index.html', { root: __dirname });
});

答案 5 :(得分:3)

通过编写更少的代码来实现此目的的另一种方法。

@import ".\test.less"

答案 6 :(得分:3)

根据其他答案,这是一个如何满足最普遍要求的简单示例:

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)

这也是到respond with index.html on every request的简单方法的两倍,因为我使用星号*来捕获在静态(公共)目录中找不到的所有文件;这是网络应用最常见的用例。更改为/以仅在根路径中返回索引。

答案 7 :(得分:2)

process.cwd()返回项目的绝对路径。

然后:

res.sendFile( `${process.cwd()}/public/index1.html` );

答案 8 :(得分:0)

如果要设置一次并在所有地方使用它,只需配置自己的中间件。设置应用程序时,请使用以下命令在响应对象上定义新功能:

app.use((req, res, next) => {
  res.show = (name) => {
    res.sendFile(`/public/${name}`, {root: __dirname});
  };
  next();
});

然后按以下方式使用它:

app.get('/demo', (req, res) => {
  res.show("index1.html");
});

答案 9 :(得分:0)

我使用Node.Js并遇到相同的问题...我解决了在每个脚本的开头添加一个'/'并链接到CSS静态文件的问题。

之前:

<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">

之后:

<link rel="stylesheet" href="/assets/css/bootstrap/bootstrap.min.css">

答案 10 :(得分:0)

您可以使用send而不是sendFile,这样您就不会遇到错误! 这项工程对您有帮助!

fs.readFile('public/index1.html',(err,data)=>{
if(err){
  consol.log(err);
}else {
res.setHeader('Content-Type', 'application/pdf');

用于告知浏览器您的回复是PDF类型

res.setHeader('Content-Disposition', 'attachment; filename='your_file_name_for_client.pdf');

如果您希望该文件在用户下载后立即在同一页面上打开,请在上面的代码中写入“内联”而不是附件。

res.send(data)
相关问题