X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=index.js;h=9279c877250c9e6055d142f9839dddd22dae775d;hb=4b6741491ea2b312f9ef93b13da4af525ec75b5a;hp=80526a3f6c879e14fb4a4055fe28504372f0d489;hpb=1cc6597bbdff00d6355c83069edb891cefa57c28;p=zombocom-ai diff --git a/index.js b/index.js index 80526a3..9279c87 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + const express = require('express'); const app = express(); const http = require('http'); @@ -8,6 +10,23 @@ const port = 2122; comments = []; +// Save comments when server is shutting down +function cleanup() { + fs.writeFileSync('zombocom-state.json', JSON.stringify(comments), (error) => { + if (error) + throw error; + }) +} + +// And connect to that on either clean exit... +process.on('exit', cleanup); + +// ... or on a SIGINT (control-C) +process.on('SIGINT', () => { + cleanup(); + process.exit(); +}); + app.get('/index.html', (req, res) => { res.sendFile(__dirname + '/index.html'); });