简体   繁体   English

当文件以点开头时,静态服务给出404

[英]Serve-static gives 404 when a file starts with dot

I have an app in production from an year. 我一年有一个应用在生产中。 It has a functionality to import and later provide zipped data files. 它具有导入和稍后提供压缩数据文件的功能。 Today I tested the app with different data where the file name starts with a dot. 今天,我用不同的数据对应用程序进行了测试,文件名以点开头。 The app successfully accepted, zipped, and saved the file, however when the front-end requested the same file, the app's back-end gived 404. 该应用程序成功接受,压缩并保存了文件,但是当前端请求相同文件时,该应用程序的后端给出了404。

I checked that the file actually exists at the requested url. 我检查了文件是否确实存在于请求的URL中。 When I replace the leading dot with lower dash, it works. 当我用下划线代替前导点时,它可以工作。

Here is my app code: 这是我的应用代码:

const app = require('express')();
const serveStatic = require('serve-static');
const path = require('path');

app.use(serveStatic(path.join(__dirname, 'public/data')));

I'm receiving the following error: 我收到以下错误:

在此处输入图片说明

The file is available at the destination: 该文件在目标位置可用:

在此处输入图片说明

All files without the leading dot work. 没有前导点的所有文件都可以工作。 All files with leading dot give 404. 所有前导点为404的文件。

Please help me to solve the problem. 请帮我解决问题。

You can serve hidden files (those that start with a dot) with serve-static with the dotfiles option set to 'allow' (by default it's set to 'ignore' ). 您可以使用dot serve-static dotfiles选项设置为'allow' (默认情况下将其设置为'ignore' ),通过serve-static提供隐藏文件(以点开头的文件)。

app.use(serveStatic(path.join(__dirname, 'public/data'), {
  dotfiles: 'allow'
}));

See: https://ewiggin.gitbooks.io/expressjs-middleware/content/serve-static.html 参见: https : //ewiggin.gitbooks.io/expressjs-middleware/content/serve-static.html

It's the same with express.static middleware: express.static中间件相同:

app.use(express.static(path.join(__dirname, 'public/data'), {
  dotfiles: 'allow'
}));

See: https://expressjs.com/en/api.html 请参阅: https//expressjs.com/en/api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM