问题
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