NodeJs End
1. Source protection
> The source code of the project involves node End background code , And front end code , The source code protection measures are as follows :
> 1: node: Code obfuscation + encryption + Compress
> 2: client : What is provided to the customer is the compiled code
> 3: node The client and the compiled client share a configuration file ; The difficulty is that the client is compiled code , When the configuration file is updated, the client cannot recognize it , Therefore, the dynamic loading method is adopted , Here are the specific steps .
Because the client and node The server involves configuration , And the client code should be sent to the client after compilation , Therefore, if the customer modifies the configuration according to his own business scenario , The compiled client code does not recognize the latest configuration file ;node Because the source code is provided , So you can get the latest configuration . In this case, the solution is as follows :
Define a global configuration file in the root directory of the entire project config.json.
{
"production":{
"HOST": "https://nfs-src.sf8cloud.com"
},
"test":{
"HOST": "https://test-nfs.sf8cloud.com"
},
"development":{
"HOST": "https://dev-nfs.sf8cloud.com"
}
}
node The end is direct require introduce , according to process Start the environment selection in different environments in the configuration file host Address ;
Focus on the client , How does the compiled client get the update of the configuration file ?
The following code , In fact, it is obtained dynamically through request , Of course /envInfo This request is in Node Add a EnvController.js( The code is as follows ), stay EnvController.js The global configuration file under the changed directory is operated in , And then according to node The client starts the environment differently and sends the configuration data of the corresponding environment to the client .
axios.get('/envInfo').then(res => {
console.log('======serverConfig===',res)
Vue.prototype.ydHostConfig = res.data
// localStorage.setItem('ydHostConfig', JSON.stringify(res.data))
}).catch(err => {
console.log('======err',err)
})/**EnvController.js**/
const configInfo = require('../../serverConfig.json')
module.exports = {
/**
* Get the environment information of the server , Match the front-end environment
*/
getEnvInfo(req, res){
configInfo.env = process.env.NODE_ENV
if (process.env.NODE_ENV === 'production') {
return res.ok(configInfo.production)
} else if(process.env.NODE_ENV==='test'){
return res.ok(configInfo.test)
}else {
return res.ok(configInfo.development)
}
}
}






