Send package.json [version] to Angularjs Front end for display purposes

拟墨画扇 提交于 2019-12-25 07:59:08

问题


Is there a simple way to send the package.json file to the front end which is Angular js 1. I need to show the version the project is at. Using Gulp on a MEAN stack. Currently I have not found a way to do this.

Edit.

Not using Gulp on MEAN stack to get it I am just stating what is being used for the project to run. I though this might help.


回答1:


So I tried and this works for me.

Create a global variable in you app.js file,

global.__base = __dirname + '/';

Create a router like this,

app.get('/getVersion', function(req, res) {
    var fs = require('fs');
    var path = require('path');
    var obj;
    var pathToFile = path.resolve(__base, 'package.json');

    fs.readFile(pathToFile, 'utf8', function(err, data) {
        if (err) throw err;
        obj = JSON.parse(data);
        res.send(obj);
    });
});

You should be able to get your entire package.json in your response.




回答2:


I think you can either copy version from the package.json into needed HTML file during build process or create some endpoint which will return current version from package.json

Something like this: GET /api/version



来源:https://stackoverflow.com/questions/39706216/send-package-json-version-to-angularjs-front-end-for-display-purposes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!