OneCompiler

NodeJS version endpoint using Git hash

238

One way of versioning the webapps is using the last Git commit's hash.
If you want to build an API endpoint in NodeJS + Express where you check the last commit hash and use that as verion, you can use the following endpoint

router.get('/version', function (req, res) {
  exec('git log --pretty=format:"%h" -1', (err, stdout, stderr) => {
    if (err) {
      console.log(`error while getting last commit`, err);
      res.send('Unable to fetch last commit hash');
    } else {
      res.send(stdout);
    }
  });
});