Preserving Nodejs apps with the use of jscrambler
Posted By : Manish Nayal | 23-Dec-2020
Developing Node.js applications that have critical logic and thus we want to prevent reverse-engineering, authorized violations, and interference, Jscrambler is a necessity.Node.js is an exceptionally popular and open-source JavaScript RE to create server-side apps.
Firstly, We have to install the latest version of npm on the local.
npm update -g npm
After that, Let's go ahead and use a very simple "Hello World" Express app. First, let's install Express:
npm install express --save
Then, we will create an app.js file in our project's root folder using the below code as provided in the Express website:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
A server is started by the app which listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/) or route.
If we run the app with:
node app.js
Also Read: Top 5 Software Tools a Web Developer Should Have.
Integrating Jscrambler
Grunt: Grunt is a JavaScript task runner with the aim of integrating repetitive tasks like mitigation, collection, unit testing, code safety, etc. In this instance, it is a profitable option to make certain that the source code of your Node.js app is always protected at build time.
We will begin with Grunt by installing it as a dev dependency as shown below:
npm install grunt --save-dev
Next, we will create a configuration file for Grunt, Gruntfile.js. This file contains our project and Grunt task configuration and loads Grunt plugins and tasks.To keep things simple, let's set up a basic Gruntfile:
module.exports = function(grunt) {grunt.initConfig({ pkg: grunt.file.readJSON('package.json')});};
Now, we will add the Jscrambler Grunt plugin:
npm install grunt-jscrambler --save-dev
Now that the plugin is installed, we will add the following line at the bottom to enable it in our Gruntfile:
grunt.loadNpmTasks('grunt-jscrambler');
Right after this line, we have to set the "default" Grunt task with:
grunt.registerTask('default', ['jscrambler']);
After its completion, we must also specify the task itself. We will do that by using some parts of the jscrambler.json file we downloaded earlier: accessKey, secretKey, applicationId, and the params array. The final result of our Gruntfile.js file will be somewhat like this:
module.exports = function(grunt) {grunt.initConfig({pkg: grunt.file.readJSON('package.json'),
jscrambler: { main: {options: {keys: {
accessKey: 'YOUR_ACCESS_KEY',
secretKey: 'YOUR_SECRET_KEY'
},applicationId: 'YOUR_APPLICATION_ID',
params: [
{ "name": "objectPropertiesSparsing"},
{ "name": "variableMasking" },
{ "name": "whitespaceRemoval” },
{ "name": "identifiersRenaming","options": {"mode": "SAFEST” } },
{"name": "dotToBracketNotation"},
{ "name": "stringConcealing"},
{ "name": "functionReordering"},
{ "options": { "freq": 1,"features": [ "opaqueFunctions”]
}, "name": "functionOutlining },
{ "name": "propertyKeysObfuscation"
"options": {"encoding": ["hexadecimal"]} },
{ "name": "regexObfuscation" },
{ "name": "booleanToAnything"}] },
files: [{expand: true, src: ['app.js'], dest: 'dist/'},
] }}});
grunt.loadNpmTasks('grunt-jscrambler');
grunt.registerTask('default', ['jscrambler']);
};
Upon looking closely at files array, we will find that Jscrambler will use the app.js file to protect it and then place the protected version on the dist/ folder. You can tweak them as per your project's objectives. Now, all that's left is to make sure that our build process is using Grunt. For our case, we should ensure the availability of a script in our package.json file to build our app using Grunt:
"scripts": { "build": "grunt" },
We're now ready to run our build:
npm run build
Henceforth, If we will check our /dist/app.js file, we will see that it has been obfuscated with Jscrambler.
Also Read: An Introduction To Deno.JS
We are a 360-degree software development company that provides complete web and mobile app development solutions for varied project requirements. Our end-to-end SaaS app development services address your mission-critical project requirements through scalable, responsive, and feature-rich software applications that are easy to scale. We carefully analyze your project requirements and formulate effective strategies to build enterprise-grade web and mobile applications for multiple platforms. For more info, contact us at [email protected].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manish Nayal
He likes making stuff on the Internet and specialises in designing digital products such as websites and web apps. He is frontend developer having good knowledge of various frontend technologies.