首页 > 解决方案 > 使用 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 文件?提前致谢

标签: javascriptnode.js

解决方案


const test = require('path/to/external-json/the-file.json');

app.get("/test", (req, res) => {
    console.log(test);
    res.jsonp({
        test: test
    });
});

推荐阅读