使用硬编码数据的简单 Express.js 后端,交换使用 JSON-file
Simple Express.js backend working with hard coded data, swapping to use JSON-file
我正在使用 Express 构建一个简单的后端。一切正常。但是我在 server.js 文件中硬编码了示例数据数组(熊)。我已将熊数据转换为外部 JSON 文件。从这个 JSON-file.
中获取相同的 bears-array 的最佳实践是什么?
我知道我可以在顶部使用 import JSONdata from "./data/DataOfBears.json";。但是如何替换 const bears =[] -section?
const express = require("express");
const app = express();
const port = 5000;
app.get("/api/bears", (req, res) => {
const bears = [
{
id: 1,
name: "Kodiak bear",
nameLatin: "Ursus arctos middendorffi",
description:
"The Kodiak bear (Ursus arctos middendorffi), also known as the Kodiak brown bear, sometimes the Alaskan brown bear, inhabits the islands of the Kodiak Archipelago in southwest Alaska",
kingdom: "Animalia",
image:
"https://media.istockphoto.com/photos/bear-isolated-on-white-background-picture-id500565090?k=6&m=500565090&s=612x612&w=0&h=AYpiv8hOMO63fcBoitA-YrFM_V7pBNQKErxsZP-8KkM="
},
{
id: 2,
name: "Cinnamon bear",
nameLatin: "Ursus americanus cinnamomum",
description:
"The cinnamon bear (Ursus americanus cinnamomum) is both a color phase and subspecies of the American black bear, native to central and western areas of the United States and Canada",
kingdom: "Animalia",
image:
"https://media.istockphoto.com/photos/bear-isolated-on-white-background-picture-id500565090?k=6&m=500565090&s=612x612&w=0&h=AYpiv8hOMO63fcBoitA-YrFM_V7pBNQKErxsZP-8KkM="
}
];
res.json(bears);
});
app.listen(port, () => console.log(`Server started, port ${port}`));
可以直接通过express
解析传递JSON文件
const data = require('/path/to/data.json')
app.get("/api/bears", function (req, res) {
res.json(data);
})
我正在使用 Express 构建一个简单的后端。一切正常。但是我在 server.js 文件中硬编码了示例数据数组(熊)。我已将熊数据转换为外部 JSON 文件。从这个 JSON-file.
中获取相同的 bears-array 的最佳实践是什么?我知道我可以在顶部使用 import JSONdata from "./data/DataOfBears.json";。但是如何替换 const bears =[] -section?
const express = require("express");
const app = express();
const port = 5000;
app.get("/api/bears", (req, res) => {
const bears = [
{
id: 1,
name: "Kodiak bear",
nameLatin: "Ursus arctos middendorffi",
description:
"The Kodiak bear (Ursus arctos middendorffi), also known as the Kodiak brown bear, sometimes the Alaskan brown bear, inhabits the islands of the Kodiak Archipelago in southwest Alaska",
kingdom: "Animalia",
image:
"https://media.istockphoto.com/photos/bear-isolated-on-white-background-picture-id500565090?k=6&m=500565090&s=612x612&w=0&h=AYpiv8hOMO63fcBoitA-YrFM_V7pBNQKErxsZP-8KkM="
},
{
id: 2,
name: "Cinnamon bear",
nameLatin: "Ursus americanus cinnamomum",
description:
"The cinnamon bear (Ursus americanus cinnamomum) is both a color phase and subspecies of the American black bear, native to central and western areas of the United States and Canada",
kingdom: "Animalia",
image:
"https://media.istockphoto.com/photos/bear-isolated-on-white-background-picture-id500565090?k=6&m=500565090&s=612x612&w=0&h=AYpiv8hOMO63fcBoitA-YrFM_V7pBNQKErxsZP-8KkM="
}
];
res.json(bears);
});
app.listen(port, () => console.log(`Server started, port ${port}`));
可以直接通过express
解析传递JSON文件const data = require('/path/to/data.json')
app.get("/api/bears", function (req, res) {
res.json(data);
})