javascript - 使用 node.js 访问服务器端的 Json 文件
问题描述
我有一个网站,它显示来自本地主机的 json 数据。目前 json 数组位于 js 服务器文件中。但是我希望它是一个外部文件。
var test = [{
id: 0,
test_id: "Password test",
pass: 1,
fail: 5,
time: 0.03,
pass_fail: 20,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 1,
test_id: "Password test",
pass: 1,
fail: 5,
time: 0.03,
pass_fail: 20,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 2,
test_id: "Form testing",
pass: 10,
fail: 0,
time: 0.09,
pass_fail: 100,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 3,
test_id: "Login Test 2",
pass: 15,
fail: 0,
time: 0.04,
pass_fail: 100,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 4,
test_id: "Pen Test",
pass: 119,
fail: 2,
time: 0.04,
pass_fail: 0,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 5,
test_id: "Pen Test 2",
pass: 19,
fail: 22,
time: 0.08,
pass_fail: 1,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 6,
test_id: "Pen Test 3",
pass: 119,
fail: 2,
time: 0.04,
pass_fail: 40,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 7,
test_id: "Login Test 4",
pass: 119,
fail: 2,
time: 0.04,
pass_fail: 0,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 8,
test_id: "Pen Test 4",
pass: 119,
fail: 210,
time: 0.04,
pass_fail: 80,
comments: [{
comment: "",
commentuser: ""
}, ]
},
{
id: 9,
test_id: "Pen Test 5",
pass: 12,
fail: 2,
time: 0.04,
pass_fail: 0,
comments: [{
comment: "",
commentuser: ""
}, ]
}
];
这是json的get请求,
app.get("/test", (req, res) => {
console.log(test);
res.jsonp({
test: test
});
});
如何在获取请求中链接存在数组测试的外部 Json 文件?提前致谢
解决方案
const test = require('path/to/external-json/the-file.json');
app.get("/test", (req, res) => {
console.log(test);
res.jsonp({
test: test
});
});
推荐阅读
- python - 如何显示使用 pytube 下载 YouTube 视频的进度?
- c# - [已解决]我怎样才能得到
在 asp:repeater 中单击 div 的 div 中的值?
- azure - 天蓝色逻辑应用程序设计器中的“将自适应卡发布到团队频道并等待响应”错误
- codenameone - 找不到档案 pod 和 Podfile.lock
- anaconda - 我可以在离开 conda (Ubuntu) 时删除 anaconda 吗?
- c - 这里的“分配内存块的开始”是什么意思?
- html - 左侧的角度下拉按钮
- xcb - xcb 内存管理:我应该在迭代器中释放数据字段吗?
- google-sheets - 我在excel中为条件格式制作了一个自定义公式,它运行良好,但在谷歌表格中的工作方式不同
- php - MySql (XAMPP) 有时表中没有数据