gpt4 book ai didi

node.js - ExpressJs 服务器以及命令行界面

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:59 24 4
gpt4 key购买 nike

我正在使用 express.js 开发 REST API 服务器,但我希望允许开发人员通过命令行发出命令。

它是一个开发服务器,读取config.json、创建端点并将数据存储在内存中。

我想让开发人员能够在不重新启动 Express 服务器的情况下重新加载基础数据。

有什么想法吗?

最佳答案

我只是想到了一个在我看来更优雅的解决方案来解决您可能感兴趣的问题:让服务器在 config.json 文件发生更改时自动重新加载它。这在 Node.js 中实现起来很简单:

const fs = require("fs"),
path = require("path");

var filePath = path.join(__dirname, "config.json");
fs.watch(filePath, function(event) {
if (event === "change") {
var file = require(filePath); // require loads JSON as a JS object
// do something with the newly loaded file here
console.log("Detected change, config file automatically reloaded.");
}
});

关于node.js - ExpressJs 服务器以及命令行界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com