Node.js中带有expressjs和服务静态的服务静态.json文件

问题描述 投票:4回答:2

我有以下代码:

...
var servceStatic = require("serve-static");
var app = express();

app.use(express.compress());
app.use(servceStatic('static'));
...

以某种方式,它可以管理所有类型的文件,但以“ .json”结尾的文件除外。为什么会这样?

json node.js static-files
2个回答
1
投票

您不需要此模块serve-static,因为它是在express中内置的:

创建一个公共文件夹,而不是在实例化express之后,仅将此行添加到您的代码中:

var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));

这应该分发所有文件,包括JSON文件。


0
投票

serve-static#index:默认情况下,此模块将发送“ index.html”文件以响应目录上的请求。禁用此设置false或提供新的索引传递首选的字符串或数组订单。

var path = require('path');
app.use(express.static(path.join(__dirname, 'public', {
    'index': ['index.json', 'index.html', 'index.htm'],
}));
© www.soinside.com 2019 - 2024. All rights reserved.