[英]Serve static .json file in Nodejs with expressjs and serve-static
I Have the following code:我有以下代码:
...
var servceStatic = require("serve-static");
var app = express();
app.use(express.compress());
app.use(servceStatic('static'));
...
Somehow it manages to serve all kind of files except those that end with ".json".不知何故,它设法为所有类型的文件提供服务,但那些以“.json”结尾的文件除外。 Why is this?
为什么是这样?
you don´t need this module serve-static, because it is build in in express:你不需要这个模块serve-static,因为它是在express中内置的:
create a public folder and than just add this line to your code after your instantiation of express:创建一个公共文件夹,而不是在实例化 express 后将此行添加到您的代码中:
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
This should hand out all your files including the JSON files.这应该分发您的所有文件,包括 JSON 文件。
serve-static#index : By default this module will send “index.html” files in response to a request on a directory.
serve-static#index :默认情况下,此模块将发送“index.html”文件以响应对目录的请求。 To disable this set false or to supply a new index pass a string or an array in preferred order.
要禁用此设置 false 或提供新索引,请按首选顺序传递字符串或数组。
var path = require('path');
app.use(express.static(path.join(__dirname, 'public', {
'index': ['index.json', 'index.html', 'index.htm'],
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.