[英]How can I read values from a static config.json file to TypeScript in a Vue file after running build:electron in Vue CLI 3?
[英]API in NextJS that serves a JSON file, can I edit this file (after build, while running) and make the API serve the updated file?
很抱歉问题中的措辞。 可能我最大的问题是不知道如何正确表达它,因为我无法从谷歌那里得到任何答案。
在 Next.js 中使用 api 路由,我想提供一个data.json
文件。 这没有问题,但是我也希望能够在之后编辑此文件,并让 api 反映更新的内容。 如果我编辑文件 api 仍然返回旧文件,那么它在构建和运行后仍然存在,我什至可以删除它。 我认为这是因为 Next.js 在构建时制作了文件的副本,并将其放在 .next 目录中的某个位置(?),但在那里找不到它。
以下是我正在做的事情的简化版本:
/pages
/api
/info.js
/data
/data.json <- file I want to use with the api
pages/api/info.js
export default function (req, res) {
const data = require('../../data/data.json')
// Do stuff
res.status(200).json(data.something)
}
非常感谢对此的任何指导
使用require
在任何 Node 应用程序中包含文件肯定会将 json 文件与应用程序运行时间或构建时间联系起来。
您描述的功能听起来像static 文件服务,但接下来也会缓存这些文件。
尝试读取 API 中的文件
const fsp = require('fs').promises
export default async function (req, res) {
try {
const file_data = await fsp.readFile('../../data/data.json')
const json_data = JSON.parse(file_data)
// Do stuff
res.status(200).json(data.something)
}
catch (error) {
console.log(error)
res.status(500).json({ error: 'Error reading data' })
}
}
在开发中,您可以通过在 static 文件更新时触发重建来解决此问题。
如果您想独立于构建的应用程序更新数据文件,它们可能需要单独托管到下一个构建。 如果您next export
一个完整的 static 站点,您可能已经有一个地方可以放置 static 文件。 如果不是,您可以使用另一个节点进程或web 服务器或类似S3的东西来提供数据文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.