假设我编写了一个模块 handler.js 作为 Express.js 中间件:
var info = {};
infohandler.setinfo = function(i){ info[i] = Math.random(); }
infohandler.saveinfo = function(){ fs.writeFileSync("info.json", info); }
function handler(req, res, next){ // here are some operation call to infohandler according to req. }
module.exports = handler;
在 app.js 中,我导入了这个 handler.js:
var handler = require("handler");
我想在服务器关闭之前将一些信息保存到文件中。我怎样才能做到这一点?