Skip to content

Instantly share code, notes, and snippets.

@ttseng
Created November 1, 2019 03:16
Show Gist options
  • Save ttseng/57e85cc0b7e9bda2c87c38c5d548bfb9 to your computer and use it in GitHub Desktop.
Save ttseng/57e85cc0b7e9bda2c87c38c5d548bfb9 to your computer and use it in GitHub Desktop.
app.post('/updateCounter', function (request, response){
// get current value
console.log('get current value');
counterDb.all('SELECT * from Counter ORDER BY id DESC LIMIT 1', function(err, row){
if(!err && row[0] && row[0].id){
const val = row[0].val;
console.log('value', val);
const id = row[0].id;
// store a new value
const sql = `UPDATE Counter SET val = ? WHERE id = ?`;
const data = [val+1, id];
counterDb.run(sql, data, function(err){
if(err) return console.log(err.message);
response.send({message: 'success'});
});
}
});
});
app.get('/getCounter', function(request, response){
console.log('getCounter');
counterDb.all('SELECT * from Counter ORDER BY id DESC LIMIT 1', function(err, row){
if(!err && row[0] && row[0].val){
console.log('value', row[0].val);
response.send(JSON.stringify(row[0].val));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment