简体   繁体   中英

How i Can Update my Json data in Express.js

I need help:

I saved my Discord bot ping in a JSON file and I want to show the ping from the JSON file in my web server using HTML. I am using Express.js.

This here is a picture of the data but it's not getting updated when it changes in the JSON file.

在此处输入图像描述

This in my express project:

app.get('/', function(req, res){
    const pingsBots = require('./Database/BotPing.json')

    const pingofbot = pingsBots.thepingofbot.botping

    let file = fs.readFileSync(path.join(__dirname, './html/', 'main.html'), { encoding: 'utf-8' })
    file = file.replace("$$theping$$", pingofbot)

    
    res.sendFile(path.join(__dirname, './html/', 'main.html'))
    res.send(file)
    
})

And this in ready method in my bot:

setInterval(function(){
        let Pusher = JSON.parse(fs.readFileSync('./Database/BotsPing.json', 'utf8'));
        Pusher["thepingofbot"] = {
            botping: `${client.ws.ping}`
        }
        async function SavePingData(path, JSONdata){
            fs.writeFileSync(path, JSON.stringify(JSONdata, null, 2))
        }
        SavePingData('./Database/BotsPing.json', Pusher)
    }, 1000)

Btw: Express project not with the bot project.

app.get('/', function(req, res){
    const pingsBots = require('./Database/BotPing.json')
    const pingofbot = pingsBots.thepingofbot.botping
    let file = fs.readFileSync(path.join(__dirname, './html/', 'main.html'), { encoding: 'utf-8' })
    file = file.replace("$$theping$$", pingofbot) 
    res.sendFile(path.join(__dirname, './html/', 'main.html'))
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM