tag:blogger.com,1999:blog-35972096102685319632024-03-16T07:45:07.199-07:00Lets InnovateJitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.comBlogger57125tag:blogger.com,1999:blog-3597209610268531963.post-21848697684603599952024-02-29T00:52:00.000-08:002024-02-29T00:56:49.896-08:00Sequelize in Node.js Application | Sequelize in Express Application | Establishing Database Connections in Node.js<p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; font-size: 16px; white-space-collapse: preserve;">Today, I am going to discuss establishing a database connection in Node.js. I have already installed MySQL Server 8.0 on my local machine. I will use an ORM (Object Relational Mapping) framework named Sequelize to connect with the database in my Node.js and Express application. </span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, Helvetica Neue, Arial, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji" style="color: #0d0d0d;"><span style="background-color: white; white-space-collapse: preserve;"><u><b>Note</b></u>: </span></span><b style="color: #0d0d0d; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space-collapse: preserve;">I have created a video tutorial about the same topic. Below is the link of the video tutorial.</b></p><p><a href="https://youtu.be/ClssJqN6xsQ" target="_blank">Video tutorial of Sequelize DB connection in Node.js Application</a><br /></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; font-size: 16px; white-space-collapse: preserve;">Below are the steps to establish the database connection and interact with it.</span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step1></b> Install Sequelize and mysql2 driver npm packages in your application.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"helloworld"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"express"</span>: <span style="color: #ce9178;">"^4.18.2"</span>,</div><div> <span style="color: #9cdcfe;">"morgan"</span>: <span style="color: #ce9178;">"^1.10.0"</span>,</div><div> <span style="color: #9cdcfe;">"mysql2"</span>: <span style="color: #ce9178;">"^3.9.2"</span>,</div><div> <span style="color: #9cdcfe;">"sequelize"</span>: <span style="color: #ce9178;">"^6.37.1"</span></div><div> }</div><div>}</div></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step2></b> Create a file named as index.js in a folder named as model in your application. In this index.js file, add DB config and create an object of Sequelize. Afterwards, you need to pass this sequelize object in all your model files definition. Model files are nothing but the replicas of a particular table. Below is the code for creating connection between Sequelize and MySql DB. Here, my dbName=testdb, username=root and password=admin. As I have installed MySql in my local machine, so, hostname=localhost otherwise it would be IP address or path of your remote server having the DB.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> { <span style="color: #4ec9b0;">Sequelize</span>, <span style="color: #4fc1ff;">DataTypes</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'sequelize'</span>);</div><br /><div><span style="color: #6a9955;">// Initialize Sequelize connection</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">sequelize</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">Sequelize</span>(<span style="color: #ce9178;">'testdb'</span>, <span style="color: #ce9178;">'root'</span>, <span style="color: #ce9178;">'admin'</span>, {</div><div> <span style="color: #9cdcfe;">host</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'localhost'</span>,</div><div> <span style="color: #9cdcfe;">dialect</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'mysql'</span>, <span style="color: #6a9955;">// Specify the dialect (in this case, MySQL)</span></div><div> <span style="color: #9cdcfe;">operationsAliases</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #9cdcfe;">pool</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #9cdcfe;">max</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">5</span>,</div><div> <span style="color: #9cdcfe;">min</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">0</span>,</div><div> <span style="color: #9cdcfe;">acquire</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">30000</span>,</div><div> <span style="color: #9cdcfe;">idle</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">10000</span></div><div> }</div><div>});</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">db</span> = {};</div><div><span style="color: #4fc1ff;">db</span>.<span style="color: #9cdcfe;">sequelize</span> = <span style="color: #4fc1ff;">sequelize</span>;</div><div><span style="color: #4fc1ff;">db</span>.<span style="color: #9cdcfe;">models</span> = {};</div><div><span style="color: #4fc1ff;">db</span>.<span style="color: #9cdcfe;">models</span>.<span style="color: #9cdcfe;">User</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'./user'</span>)(<span style="color: #4fc1ff;">sequelize</span>, <span style="color: #4ec9b0;">Sequelize</span>.<span style="color: #9cdcfe;">DataTypes</span>);</div><div><span style="color: #4fc1ff;">module</span>.<span style="color: #4fc1ff;">exports</span> = <span style="color: #4fc1ff;">db</span>;</div><br /><div><span style="color: #6a9955;">// Close Sequelize connection when the Node.js process exits</span></div><div><span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'exit'</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #4fc1ff;">sequelize</span>.<span style="color: #dcdcaa;">close</span>()</div><div> .<span style="color: #dcdcaa;">then</span>(() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'Sequelize connection closed'</span>);</div><div> })</div><div> .<span style="color: #dcdcaa;">catch</span>((<span style="color: #9cdcfe;">error</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error closing Sequelize connection:'</span>, <span style="color: #9cdcfe;">error</span>);</div><div> });</div><div> });</div><div> </div><div> <span style="color: #6a9955;">// Handle Ctrl+C or SIGINT signal to gracefully close the connection</span></div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'SIGINT'</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #4fc1ff;">sequelize</span>.<span style="color: #dcdcaa;">close</span>()</div><div> .<span style="color: #dcdcaa;">then</span>(() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'Sequelize connection closed'</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">0</span>);</div><div> })</div><div> .<span style="color: #dcdcaa;">catch</span>((<span style="color: #9cdcfe;">error</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error closing Sequelize connection:'</span>, <span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">1</span>);</div><div> });</div><div> });</div><div> </div><div> <span style="color: #6a9955;">// Handle uncaught exceptions and promise rejections</span></div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'uncaughtException'</span>, (<span style="color: #9cdcfe;">error</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #4fc1ff;">sequelize</span>.<span style="color: #dcdcaa;">close</span>()</div><div> .<span style="color: #dcdcaa;">then</span>(() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'Sequelize connection closed'</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">1</span>);</div><div> })</div><div> .<span style="color: #dcdcaa;">catch</span>((<span style="color: #9cdcfe;">closeError</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error closing Sequelize connection:'</span>, <span style="color: #9cdcfe;">closeError</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">1</span>);</div><div> });</div><div> });</div><div> </div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'unhandledRejection'</span>, (<span style="color: #9cdcfe;">reason</span>, <span style="color: #9cdcfe;">promise</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #4fc1ff;">sequelize</span>.<span style="color: #dcdcaa;">close</span>()</div><div> .<span style="color: #dcdcaa;">then</span>(() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'Sequelize connection closed'</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">1</span>);</div><div> })</div><div> .<span style="color: #dcdcaa;">catch</span>((<span style="color: #9cdcfe;">closeError</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error closing Sequelize connection:'</span>, <span style="color: #9cdcfe;">closeError</span>);</div><div> <span style="color: #9cdcfe;">process</span>.<span style="color: #dcdcaa;">exit</span>(<span style="color: #b5cea8;">1</span>);</div><div> });</div><div> });</div><div> </div><div> </div></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;">If you go through the above code, I have written few blocks of code to gracefully close the DB connection if the process is failing due to any reasons like process.exit or SIGINT signal or Unhandled exception or Unhandled rejections.</span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step 3></b> Now, We should create a model file. As discussed, model is nothing but the replica of a table in your DB. I have created here a model named as User corresponding to the table Users. Here, model name and table name may be same. If these are not same then we have to specify the table name by attribute tableName: 'users'. While defining this model, I am using the sequelize object being passed from index.js file where we have created the DB connection with Sequelize.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = (<span style="color: #9cdcfe;">sequelize</span>, <span style="color: #9cdcfe;">DataTypes</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">UserModel</span> = <span style="color: #9cdcfe;">sequelize</span>.<span style="color: #dcdcaa;">define</span>(<span style="color: #ce9178;">'User'</span>, {</div><div> <span style="color: #9cdcfe;">id</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #9cdcfe;">type</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">DataTypes</span>.<span style="color: #4fc1ff;">INTEGER</span>,</div><div> <span style="color: #9cdcfe;">primaryKey</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #9cdcfe;">autoIncrement</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span></div><div> },</div><div> <span style="color: #9cdcfe;">name</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #9cdcfe;">type</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">DataTypes</span>.<span style="color: #4fc1ff;">STRING</span>,</div><div> <span style="color: #9cdcfe;">allowNull</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span></div><div> },</div><div> <span style="color: #9cdcfe;">email</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #9cdcfe;">type</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">DataTypes</span>.<span style="color: #4fc1ff;">STRING</span>,</div><div> <span style="color: #9cdcfe;">allowNull</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span></div><div> }</div><div> }, {</div><div> <span style="color: #9cdcfe;">tableName</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'users'</span>,</div><div> <span style="color: #9cdcfe;">timestamps</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span> </div><div> });</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">UserModel</span>;</div><div>}</div><br /></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step 4></b> Next, we will utilize this model file in our service class to perform CRUD operations on users table. Here, I have created functions to get list of users, add new user and delete an user. </span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> {<span style="color: #9cdcfe;">models</span>: {<span style="color: #4fc1ff;">User</span>}} = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'../model'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">getUsers</span> = <span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> () {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">users</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">User</span>.<span style="color: #dcdcaa;">findAll</span>();</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">users</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">addUsers</span> = <span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> (<span style="color: #9cdcfe;">userObj</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">insertResult</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">User</span>.<span style="color: #dcdcaa;">create</span>(<span style="color: #9cdcfe;">userObj</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">insertResult</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">deleteUser</span> = <span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> (<span style="color: #9cdcfe;">id</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">deleteResult</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">User</span>.<span style="color: #dcdcaa;">destroy</span>({<span style="color: #ce9178;">"where"</span><span style="color: #9cdcfe;">:</span> {<span style="color: #ce9178;">"id"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">id</span>}});</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">deleteResult</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = {</div><div> <span style="color: #dcdcaa;">getUsers</span>, <span style="color: #dcdcaa;">addUsers</span>, <span style="color: #dcdcaa;">deleteUser</span></div><div>}</div></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step 5></b> Next, we will create a controller class where we will use service functions to interact with DB. The controller functions will be used as middleware in route file.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">userService</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'../service/user'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">getUsers</span> = <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">users</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">userService</span>.<span style="color: #dcdcaa;">getUsers</span>();</div><div> <span style="color: #9cdcfe;">res</span>[<span style="color: #ce9178;">"result"</span>] = <span style="color: #4fc1ff;">users</span>;</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>};</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">addUsers</span> = <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">userObj</span> = {</div><div> <span style="color: #ce9178;">"name"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">name</span>,</div><div> <span style="color: #ce9178;">"email"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">email</span></div><div> };</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">insertResult</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">userService</span>.<span style="color: #dcdcaa;">addUsers</span>(<span style="color: #4fc1ff;">userObj</span>);</div><div> <span style="color: #9cdcfe;">res</span>[<span style="color: #ce9178;">"result"</span>] = <span style="color: #4fc1ff;">insertResult</span>;</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>};</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">deleteUser</span> = <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> { </div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">deleteResult</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">userService</span>.<span style="color: #dcdcaa;">deleteUser</span>(<span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">query</span>.<span style="color: #9cdcfe;">id</span>);</div><div> <span style="color: #9cdcfe;">res</span>[<span style="color: #ce9178;">"result"</span>] = <span style="color: #4fc1ff;">deleteResult</span>;</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>};</div><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = {</div><div> <span style="color: #dcdcaa;">getUsers</span>, <span style="color: #dcdcaa;">addUsers</span>, <span style="color: #dcdcaa;">deleteUser</span></div><div>}</div></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>Step 6></b> Create a route file and call the controller middleware functions in each routes.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">router</span> = <span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">Router</span>();</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">userCtrl</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'../controller/user'</span>);</div><div><span style="color: #569cd6;">const</span> { <span style="color: #dcdcaa;">routeMiddleware</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'../myMiddleware/customMiddleware'</span>); </div><br /><div><span style="color: #4fc1ff;">router</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/helloWorld'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'Hello, World!'</span>);</div><div>});</div><br /><div><span style="color: #4fc1ff;">router</span>.<span style="color: #dcdcaa;">delete</span>(<span style="color: #ce9178;">'/deleteuser'</span>, <span style="color: #9cdcfe;">userCtrl</span>.<span style="color: #dcdcaa;">deleteUser</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> { <span style="color: #4fc1ff;">name</span>, <span style="color: #4fc1ff;">email</span> } = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">query</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">json</span>({<span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span>, <span style="color: #ce9178;">"result"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">res</span>.<span style="color: #9cdcfe;">result</span>});</div><div>});</div><br /><div><span style="color: #4fc1ff;">router</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/users'</span>, <span style="color: #9cdcfe;">userCtrl</span>.<span style="color: #dcdcaa;">getUsers</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">json</span>({<span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span>, <span style="color: #ce9178;">"result"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">res</span>.<span style="color: #9cdcfe;">result</span>});</div><div>});</div><br /><div><span style="color: #4fc1ff;">router</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/adduser'</span>, <span style="color: #9cdcfe;">userCtrl</span>.<span style="color: #dcdcaa;">addUsers</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> { <span style="color: #4fc1ff;">name</span>, <span style="color: #4fc1ff;">email</span> } = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">json</span>({<span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span>, <span style="color: #ce9178;">"result"</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">res</span>.<span style="color: #9cdcfe;">result</span>});</div><div>});</div><br /><div><span style="color: #4fc1ff;">module</span>.<span style="color: #4fc1ff;">exports</span> = <span style="color: #4fc1ff;">router</span>;</div><br /></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;">Step 7> One most important step that we need to do here is to trigger sync() function of Sequelize in your app.js file (where you are initializing your server) to get all your defined models synced with DB.</span></p><div style="background-color: #1e1e1e; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">bodyParser</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'body-parser'</span>);</div><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">morgan</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'morgan'</span>);</div><div><span style="color: red;">const db = require('./model');</span></div><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> { <span style="color: #dcdcaa;">errorHandler</span>, <span style="color: #dcdcaa;">applicationMiddleware</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'./myMiddleware/customMiddleware'</span>);</div><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">routes</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'./route/routes'</span>);</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div style="color: #d4d4d4;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">3000</span>;</div><span style="color: #d4d4d4;"><br /></span><div><span style="color: red;">(async () => {</span></div><div><span style="color: red;"> await db.sequelize.sync();</span></div><div><span style="color: red;">})();</span></div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #6a9955;">// Built-in Middleware to parse JSON data in the request body</span></div><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #4fc1ff;">bodyParser</span>.<span style="color: #dcdcaa;">json</span>());</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #6a9955;">//Using Third-party middleware function for logging</span></div><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">morgan</span>(<span style="color: #ce9178;">'dev'</span>));</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #6a9955;">// Application level middleware</span></div><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">applicationMiddleware</span>);</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #6a9955;">// Use the routes in your app</span></div><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #ce9178;">'/'</span>, <span style="color: #4fc1ff;">routes</span>);</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #6a9955;">//Using error handler middleware</span></div><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">errorHandler</span>);</div><span style="color: #d4d4d4;"><br /></span><div style="color: #d4d4d4;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div style="color: #d4d4d4;"> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Server is running on http://localhost:</span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">port</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div style="color: #d4d4d4;">});</div><span style="color: #d4d4d4;"><br /></span></div><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><b>I have created a video tutorial about the same topic. Below is the link of the video tutorial.</b></span></p><p><a href="https://youtu.be/ClssJqN6xsQ" target="_blank">Video tutorial of Sequelize DB connection in Node.js Application</a><br /></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p><p><span face="Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"" style="background-color: white; color: #0d0d0d; white-space-collapse: preserve;"><br /></span></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-20750890750584312682023-06-11T08:59:00.000-07:002023-06-11T08:59:43.401-07:00Create your ChatBot using NodeJs and ReactJs with ChatGPT API in 10 minutes<p> Today, I am going to show here how easily we can develop our chatbot with in few minutes using ChatGPT api. Here, I am using followings:</p><p></p><ol style="text-align: left;"><li>ReactJs for creating user front end</li><li>NodeJs for server api interacting with ChatGPT API</li><li>ChatGPT API</li></ol><div>Here, we have to complete 3 tasks:</div><div><br /></div><div>Task 1: Create your ChatGPT API secret key to be used in your code. You need to follow below steps:</div><div><ol style="text-align: left;"><li>Open the url https://openai.com/ and login here using your Google Id or create your own login Id and password.</li><li>Create your ChatGPT secret key by clicking on the "Create new Secret key" in the url given in below screenshot. Copy and paste this key in your notepad file. You will not be able to retrieve it again. </li></ol><div><span> </span><span> </span><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEjxgXilkzIM8ipBfAp5qfoG3930sucnZSRbOrsRdY9o-W6P1iXEmxbEUQ9sOUrVhBW89zNAtDjPzzcBdiHN7XTjNfzHGDV2vKUt0J65iNqgdU9ePTBGnfBQzSKt4u1bniVOsxlfJDPGuk5JdRJMx4UXIw8FlrW6uLw5c7UzT2bsGPJpoj-QkHOWKQdn" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="437" data-original-width="1176" height="175" src="https://blogger.googleusercontent.com/img/a/AVvXsEjxgXilkzIM8ipBfAp5qfoG3930sucnZSRbOrsRdY9o-W6P1iXEmxbEUQ9sOUrVhBW89zNAtDjPzzcBdiHN7XTjNfzHGDV2vKUt0J65iNqgdU9ePTBGnfBQzSKt4u1bniVOsxlfJDPGuk5JdRJMx4UXIw8FlrW6uLw5c7UzT2bsGPJpoj-QkHOWKQdn=w472-h175" width="472" /></a></div><div><br /></div><div><br /></div><div>Task2: Create Node Js api: Here, we should follow the below steps:</div><div><ol style="text-align: left;"><li>Install NodeJs and NPM</li><li>Create a node js project by command npm init -y</li><li>Install express and axios dependencies.</li></ol><div><span> </span><span> </span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">{</span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"chatnode"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">"Jitendra Kumar Singh"</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"axios"</span>: <span style="color: #ce9178;">"^1.4.0"</span>,</div><div> <span style="color: #9cdcfe;">"express"</span>: <span style="color: #ce9178;">"^4.18.2"</span></div><div> }</div><div>}</div><br /></div></div><div><br /></div><div>4. Create a new file server.js file where you will define port, start server and create api for interacting with ChatGPT api</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">axios</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'axios'</span>);</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">3001</span>; <span style="color: #6a9955;">// Set your desired port number</span></div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #569cd6;">function</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #dcdcaa;">next</span>) {</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">header</span>(<span style="color: #ce9178;">"Access-Control-Allow-origin"</span>, <span style="color: #ce9178;">"http://localhost:3000"</span>)</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">setHeader</span>(<span style="color: #ce9178;">'Access-Control-Allow-Methods'</span>, <span style="color: #ce9178;">"GET,POST,OPTIONS"</span>)</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">header</span>(<span style="color: #ce9178;">"Access-Control-Allow-Headers"</span>, <span style="color: #ce9178;">"Origin, X-Requested-With, Content-Type, Accept"</span>)</div><div> <span style="color: #dcdcaa;">next</span>();</div><div>})</div><br /><div><span style="color: #6a9955;">// Middleware to parse JSON request bodies</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">json</span>());</div><br /><div><span style="color: #6a9955;">// POST endpoint to handle user queries</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/api/query'</span>, <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> { <span style="color: #4fc1ff;">query</span> } = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><br /><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #6a9955;">// Make a POST request to the ChatGPT API</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">response</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'https://api.openai.com/v1/chat/completions'</span>, {</div><div> <span style="color: #9cdcfe;">model</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'gpt-3.5-turbo'</span>,</div><div> <span style="color: #9cdcfe;">messages</span><span style="color: #9cdcfe;">:</span> [{ <span style="color: #9cdcfe;">role</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'system'</span>, <span style="color: #9cdcfe;">content</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'You are a helpful assistant.'</span> }, { <span style="color: #9cdcfe;">role</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'user'</span>, <span style="color: #9cdcfe;">content</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">query</span> }],</div><div> }, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer YOUR_CHATGPT_API_SECRET_KEY'</span>, <span style="color: #6a9955;">// Replace with your ChatGPT API key</span></div><div> <span style="color: #ce9178;">'Content-Type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span>,</div><div> },</div><div> });</div><br /><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">json</span>({ <span style="color: #9cdcfe;">reply</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">response</span>.<span style="color: #9cdcfe;">data</span>.<span style="color: #9cdcfe;">choices</span>[<span style="color: #b5cea8;">0</span>].<span style="color: #9cdcfe;">message</span>.<span style="color: #9cdcfe;">content</span> });</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error:'</span>, <span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">response</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">500</span>).<span style="color: #dcdcaa;">json</span>({ <span style="color: #9cdcfe;">error</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'An error occurred while processing the request.'</span> });</div><div> }</div><div>});</div><br /><div><span style="color: #6a9955;">// Start the server</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Server is running on http://localhost:</span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">port</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div>});</div><br /></div></div><br />5) Start your node js server by npm start</div></div><div><br /></div><div>Task 3: Create ReactJS user interface: Here, we need to follow below steps:</div><div><br /></div><div><ol style="text-align: left;"><li>In another separate directory, create ReactJs project by using command npx create-react-app your_project_name</li><li>Once installation is done, then create dependency of axios. Your final package.json file would be like</li></ol><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"client"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"0.1.0"</span>,</div><div> <span style="color: #9cdcfe;">"private"</span>: <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"@testing-library/jest-dom"</span>: <span style="color: #ce9178;">"^5.16.5"</span>,</div><div> <span style="color: #9cdcfe;">"@testing-library/react"</span>: <span style="color: #ce9178;">"^13.4.0"</span>,</div><div> <span style="color: #9cdcfe;">"@testing-library/user-event"</span>: <span style="color: #ce9178;">"^13.5.0"</span>,</div><div> <span style="color: #9cdcfe;">"axios"</span>: <span style="color: #ce9178;">"^1.4.0"</span>,</div><div> <span style="color: #9cdcfe;">"concurrently"</span>: <span style="color: #ce9178;">"^8.2.0"</span>,</div><div> <span style="color: #9cdcfe;">"react"</span>: <span style="color: #ce9178;">"^18.2.0"</span>,</div><div> <span style="color: #9cdcfe;">"react-dom"</span>: <span style="color: #ce9178;">"^18.2.0"</span>,</div><div> <span style="color: #9cdcfe;">"react-scripts"</span>: <span style="color: #ce9178;">"5.0.1"</span>,</div><div> <span style="color: #9cdcfe;">"web-vitals"</span>: <span style="color: #ce9178;">"^2.1.4"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"start"</span>: <span style="color: #ce9178;">"react-scripts start"</span>,</div><div> <span style="color: #9cdcfe;">"build"</span>: <span style="color: #ce9178;">"react-scripts build"</span>,</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"react-scripts test"</span>,</div><div> <span style="color: #9cdcfe;">"eject"</span>: <span style="color: #ce9178;">"react-scripts eject"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"eslintConfig"</span>: {</div><div> <span style="color: #9cdcfe;">"extends"</span>: [</div><div> <span style="color: #ce9178;">"react-app"</span>,</div><div> <span style="color: #ce9178;">"react-app/jest"</span></div><div> ]</div><div> },</div><div> <span style="color: #9cdcfe;">"browserslist"</span>: {</div><div> <span style="color: #9cdcfe;">"production"</span>: [</div><div> <span style="color: #ce9178;">">0.2%"</span>,</div><div> <span style="color: #ce9178;">"not dead"</span>,</div><div> <span style="color: #ce9178;">"not op_mini all"</span></div><div> ],</div><div> <span style="color: #9cdcfe;">"development"</span>: [</div><div> <span style="color: #ce9178;">"last 1 chrome version"</span>,</div><div> <span style="color: #ce9178;">"last 1 firefox version"</span>,</div><div> <span style="color: #ce9178;">"last 1 safari version"</span></div><div> ]</div><div> }</div><div>}</div><br /></div></div><div><br /></div></div><div>3) Then open App.js file inside folder named as src and add below code</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #c586c0;">import</span> <span style="color: #9cdcfe;">React</span>, { <span style="color: #9cdcfe;">useState</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'react'</span>;</div><div><span style="color: #c586c0;">import</span> <span style="color: #9cdcfe;">axios</span> <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'axios'</span>;</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">App</span> = () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> [<span style="color: #4fc1ff;">query</span>, <span style="color: #dcdcaa;">setQuery</span>] = <span style="color: #dcdcaa;">useState</span>(<span style="color: #ce9178;">''</span>);</div><div> <span style="color: #569cd6;">const</span> [<span style="color: #4fc1ff;">reply</span>, <span style="color: #dcdcaa;">setReply</span>] = <span style="color: #dcdcaa;">useState</span>(<span style="color: #ce9178;">''</span>);</div><br /><div> <span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">handleSubmit</span> = <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">event</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">event</span>.<span style="color: #dcdcaa;">preventDefault</span>();</div><br /><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">response</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">axios</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'http://localhost:3001/api/query'</span>, { <span style="color: #9cdcfe;">query</span> });</div><br /><div> <span style="color: #dcdcaa;">setReply</span>(<span style="color: #4fc1ff;">response</span>.<span style="color: #9cdcfe;">data</span>.<span style="color: #9cdcfe;">reply</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">error</span>(<span style="color: #ce9178;">'Error:'</span>, <span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">response</span>.<span style="color: #9cdcfe;">data</span>);</div><div> }</div><div> };</div><br /><div> <span style="color: #c586c0;">return</span> (</div><div> <span style="color: grey;"><</span><span style="color: #569cd6;">div</span><span style="color: grey;">></span></div><div> <span style="color: grey;"><</span><span style="color: #569cd6;">h1</span><span style="color: grey;">></span>My Chatbot<span style="color: grey;"></</span><span style="color: #569cd6;">h1</span><span style="color: grey;">></span></div><div> <span style="color: grey;"><</span><span style="color: #569cd6;">form</span> <span style="color: #9cdcfe;">onSubmit</span>=<span style="color: #569cd6;">{</span><span style="color: #dcdcaa;">handleSubmit</span><span style="color: #569cd6;">}</span><span style="color: grey;">></span></div><div> <span style="color: grey;"><</span><span style="color: #569cd6;">input</span> <span style="color: #9cdcfe;">type</span>=<span style="color: #ce9178;">"text"</span> <span style="color: #9cdcfe;">value</span>=<span style="color: #569cd6;">{</span><span style="color: #4fc1ff;">query</span><span style="color: #569cd6;">}</span> <span style="color: #9cdcfe;">onChange</span>=<span style="color: #569cd6;">{</span>(<span style="color: #9cdcfe;">e</span>) <span style="color: #569cd6;">=></span> <span style="color: #dcdcaa;">setQuery</span>(<span style="color: #9cdcfe;">e</span>.<span style="color: #9cdcfe;">target</span>.<span style="color: #9cdcfe;">value</span>)<span style="color: #569cd6;">}</span> <span style="color: grey;">/></span></div><div> <span style="color: grey;"><</span><span style="color: #569cd6;">button</span> <span style="color: #9cdcfe;">type</span>=<span style="color: #ce9178;">"submit"</span><span style="color: grey;">></span>Send<span style="color: grey;"></</span><span style="color: #569cd6;">button</span><span style="color: grey;">></span></div><div> <span style="color: grey;"></</span><span style="color: #569cd6;">form</span><span style="color: grey;">></span></div><div> <span style="color: #569cd6;">{</span><span style="color: #4fc1ff;">reply</span> && <span style="color: grey;"><</span><span style="color: #569cd6;">p</span><span style="color: grey;">></span><span style="color: #569cd6;">{</span><span style="color: #4fc1ff;">reply</span><span style="color: #569cd6;">}</span><span style="color: grey;"></</span><span style="color: #569cd6;">p</span><span style="color: grey;">></span><span style="color: #569cd6;">}</span></div><div> <span style="color: grey;"></</span><span style="color: #569cd6;">div</span><span style="color: grey;">></span></div><div> );</div><div>};</div><br /><div><span style="color: #c586c0;">export</span> <span style="color: #c586c0;">default</span> <span style="color: #dcdcaa;">App</span>;</div></div></div><div><br /></div><div>4) Start your UI react application by using command npm start</div><div><br /></div><div>So, finally when you start UI and Node Js server then you will get functionality as shown below:</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgHKsdgJwqAnXY7jy71wJ-MFPcCXP0in7XBQVV1-HFCdf-TY6FqVxp_xm-30BsCDw6egnsggGWL7eOcYVGWk4ZP6-xV9NvbIaXxGH2ufeFnDFTtRhUCW1JT9C6Ti60VZrl2K_qMErp9aY1NoZ3j6d2w1ujmBQ-0VBMVqOKIuZAEUWiQLJOxp_4WrgBX" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="239" data-original-width="385" height="251" src="https://blogger.googleusercontent.com/img/a/AVvXsEgHKsdgJwqAnXY7jy71wJ-MFPcCXP0in7XBQVV1-HFCdf-TY6FqVxp_xm-30BsCDw6egnsggGWL7eOcYVGWk4ZP6-xV9NvbIaXxGH2ufeFnDFTtRhUCW1JT9C6Ti60VZrl2K_qMErp9aY1NoZ3j6d2w1ujmBQ-0VBMVqOKIuZAEUWiQLJOxp_4WrgBX=w404-h251" width="404" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgs8yeXCDpiOwSyEIgoH02KlYOoX5XZaZQWFoE_uxUKIS3qdYW7_ii7bmlEZxlLDaRO_rfj1Ds7PGqCXKYXlRiZMYDrn27NY3QmI6Ofc6B9eHFW7cX5SzXJgQiY1sPo1a623hs0zZ_i2g5i8WEPa2vSsthtJgPv86egsASy1EUzF1L2HceOM5fC46J_" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="216" data-original-width="1290" height="94" src="https://blogger.googleusercontent.com/img/a/AVvXsEgs8yeXCDpiOwSyEIgoH02KlYOoX5XZaZQWFoE_uxUKIS3qdYW7_ii7bmlEZxlLDaRO_rfj1Ds7PGqCXKYXlRiZMYDrn27NY3QmI6Ofc6B9eHFW7cX5SzXJgQiY1sPo1a623hs0zZ_i2g5i8WEPa2vSsthtJgPv86egsASy1EUzF1L2HceOM5fC46J_=w480-h94" width="480" /></a></div><br /><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><br /><br /></div><div><br /></div><div><br /></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-73082254470020900392022-08-13T00:06:00.000-07:002022-08-13T00:06:39.363-07:00Assume AWS role and retrieve temporary credentials to access allowed services of AWS<p> <b>Scenario</b>: </p><p>Suppose I am an user of an AWS account with account id 1111 and I want to programmatically access a S3 bucket created in another AWS account 2222. So, how can I access it programmatically through cross account. To fulfill this scenario, we have to follow the following steps:</p><p><b>Steps</b>:</p><p></p><ol style="text-align: left;"><li>Admin of account 2222 should create a bucket say Bucket2222.</li><li>Admin of account 2222 should create an IAM Role with minimum 2 policies. One will be the Trust Policy and second with the list of accesses of the Bucket2222</li><li>Admin of account 1111 should give me the IAM access of Assume Role</li><li>In Node JS code, I have to first get the temporary credential of the IAM Role created in Account 2222 by using aws-sdk version 3 STSClient class</li><li>If needed I can cache the credential in API side to reuse it until it is expired</li><li>I will access the Bucket2222 in Account 2222 by using S3Client class of aws-sdk version 3 by passing the temporary credentials</li></ol><div>Below is the complete code to access the temporary credential of this role and then listing the bucket items:</div><div><br /></div><div><b>Package.json</b></div><div><b><br /></b></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"aws_node_poc"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">"Jitendra Kumar Singh"</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"@aws-sdk/client-s3"</span>: <span style="color: #ce9178;">"^3.142.0"</span>,</div><div> <span style="color: #9cdcfe;">"@aws-sdk/client-sts"</span>: <span style="color: #ce9178;">"^3.142.0"</span></div><div> }</div><div>}</div><br /></div></div><div><b><br /></b></div><div><b>Constants.js</b></div><div><b><br /></b></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = {</div><div> <span style="color: #ce9178;">"REGION"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"My Region name"</span>,</div><div> <span style="color: #ce9178;">"ASSUME_ROLE_ARN"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"arn:aws:iam::2222:role/my_assume_role_name"</span>,</div><div> <span style="color: #ce9178;">"TEST_BUCKET_NAME"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"Bucket2222"</span></div><div>}</div></div></div><div><b><br /></b></div><div><b>AssumeRoleAndAccessBucket.js</b></div><div><b><br /></b></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><br /><div> <span style="color: #569cd6;">const</span> { <span style="color: #4ec9b0;">S3Client</span>, <span style="color: #4ec9b0;">ListObjectsCommand</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"@aws-sdk/client-s3"</span>); </div><div> <span style="color: #569cd6;">const</span> { <span style="color: #4ec9b0;">STSClient</span>, <span style="color: #4ec9b0;">AssumeRoleCommand</span>, <span style="color: #4ec9b0;">GetCallerIdentityCommand</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"@aws-sdk/client-sts"</span>);</div><div> <span style="color: #569cd6;">const</span> { <span style="color: #9cdcfe;">REGION</span>, <span style="color: #9cdcfe;">ASSUME_ROLE_ARN</span>, <span style="color: #9cdcfe;">TEST_BUCKET_NAME</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'../../util/constants.js'</span>);</div><br /><div> <span style="color: #6a9955;">//Getting Role credentials</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">assumeRole</span> = <span style="color: #569cd6;">async</span> () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">rolecreds</span>;</div><div> <span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * The credential can be cached on api side in reddish or memcache</span></div><div><span style="color: #6a9955;"> * to reuse the credential till it is expired. This credential is having Expiration </span></div><div><span style="color: #6a9955;"> * attribute that defines the time when it is expired</span></div><div><span style="color: #6a9955;"> * We can check this time to verify whether temp credential is expired</span></div><div><span style="color: #6a9955;"> * If not then reuse it and if expired then send a hit to AWS to get new temp credential</span></div><div><span style="color: #6a9955;"> */</span></div><div> <span style="color: #6a9955;">//rolecreds = "Get credential from API cache if cached and if not expired"</span></div><br /><div> <span style="color: #c586c0;">if</span>(!<span style="color: #9cdcfe;">rolecreds</span>) {</div><div> <span style="color: #6a9955;">// Create an Amazon STS service client object.</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">stsClient</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">STSClient</span>({</div><div> <span style="color: #9cdcfe;">region</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">REGION</span></div><div> });</div><br /><div> <span style="color: #6a9955;">// Set the parameters</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">params</span> = {</div><div> <span style="color: #9cdcfe;">RoleArn</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">ASSUME_ROLE_ARN</span>, <span style="color: #6a9955;">//ARN_OF_ROLE_TO_ASSUME</span></div><div> <span style="color: #9cdcfe;">RoleSessionName</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"MyAssueRoleSessionName"</span>,</div><div> <span style="color: #9cdcfe;">DurationSeconds</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">3600</span>,</div><div> };</div><br /><div> <span style="color: #6a9955;">//Assume Role</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">data</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">stsClient</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">AssumeRoleCommand</span>(<span style="color: #4fc1ff;">params</span>));</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"Cred = "</span>, <span style="color: #9cdcfe;">data</span>);</div><div> </div><div> <span style="color: #9cdcfe;">rolecreds</span> = {</div><div> <span style="color: #9cdcfe;">accessKeyId</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">data</span>.<span style="color: #9cdcfe;">Credentials</span>.<span style="color: #9cdcfe;">AccessKeyId</span>,</div><div> <span style="color: #9cdcfe;">secretAccessKey</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">data</span>.<span style="color: #9cdcfe;">Credentials</span>.<span style="color: #9cdcfe;">SecretAccessKey</span>,</div><div> <span style="color: #9cdcfe;">sessionToken</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">data</span>.<span style="color: #9cdcfe;">Credentials</span>.<span style="color: #9cdcfe;">SessionToken</span></div><div> };</div><div> } </div><br /><div> <span style="color: #6a9955;">/*const stsParams = { credentials: rolecreds };</span></div><div><span style="color: #6a9955;"> const stsClient1 = new STSClient(stsParams);</span></div><div><span style="color: #6a9955;"> const results = await stsClient1.send(</span></div><div><span style="color: #6a9955;"> new GetCallerIdentityCommand(rolecreds)</span></div><div><span style="color: #6a9955;"> );*/</span></div><br /><div> <span style="color: #c586c0;">return</span> <span style="color: #9cdcfe;">rolecreds</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>; </div><div> } </div><div> }</div><br /><div> <span style="color: #6a9955;">//Getting Object List from Bucket</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">listObjects</span> = <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">roleCred</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #6a9955;">//Creating S3 Client</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">s3Client</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">S3Client</span>({ </div><div> <span style="color: #9cdcfe;">region</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">REGION</span>,</div><div> <span style="color: #9cdcfe;">credentials</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">roleCred</span></div><div> });</div><div> <span style="color: #6a9955;">//Getting object list from bucket</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">bucketParams</span> = { <span style="color: #9cdcfe;">Bucket</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">TEST_BUCKET_NAME</span> };</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">data</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">s3Client</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">ListObjectsCommand</span>(<span style="color: #4fc1ff;">bucketParams</span>));</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">data</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div> }</div><br /><div> <span style="color: #6a9955;">//Getting Role credential and calling function to get object list from bucket</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">listBucketObjects</span> = <span style="color: #569cd6;">async</span>() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">roleCred</span> = <span style="color: #c586c0;">await</span> <span style="color: #dcdcaa;">assumeRole</span>();</div><div> <span style="color: #6a9955;">//get object list of the bucket</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #dcdcaa;">listObjects</span>(<span style="color: #4fc1ff;">roleCred</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">result</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div> }</div><br /><div> <span style="color: #6a9955;">//Triggering function to initiate process of getting data from bucket by using Role</span></div><div> <span style="color: #dcdcaa;">listBucketObjects</span>()</div><div> .<span style="color: #dcdcaa;">then</span>(<span style="color: #9cdcfe;">data</span> <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">data</span>);</div><div> }).<span style="color: #dcdcaa;">catch</span>(<span style="color: #9cdcfe;">err</span> <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"Error in my code = "</span>, <span style="color: #9cdcfe;">err</span>);</div><div> })</div><div> </div></div></div><div><b><br /></b></div><div>This is all I am having here to deal with such scenarios. Please let me know in comment section if you facing any problem in any other scenarios, I will try to post blog related with those issues.</div><div><b><br /></b></div><div><b><br /></b></div><div><b><br /></b></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-86480799016325229102022-07-17T08:45:00.004-07:002022-07-21T03:53:29.451-07:00NFR : None Functional Requirement in Software Engineering or Application Development : Part 4: Extensibility, Observability, Maintainability<p> In this post, we are going to discuss about the remaining 3 NFRs like <b>Extensibility, Observability, Maintainability</b>. The link of all of my posts related with NFR are as follows:</p><p><b>Links</b>:</p><ul><li><a href="https://jksnu.blogspot.com/2022/03/inprogress.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 1</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 2: Scalability, Performance, Testability</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_17.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 3: Application Security</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_90.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 4: Extensibility, Observability, Maintainability</a></li></ul><p><b>Extensibility</b>: Extensibility is a measure of the ability to extend an application and the level of effort required to implement the extension. It's components are like:</p><p></p><ol style="text-align: left;"><li><b>Flexibility</b>: It defines how flexible your application is towards accepting business growing needs and new functionality changes. We can make our application more flexible by applying OOPS concepts, Dependence Injection, Applying Microservice/ Microviews architecture etc.</li><li><b>Configurability: </b>It measures the extent of configuration used in your application. We should be able to add or hide the functionality from an application by configuration. There should not be any code related with any 3rd party application or vendor. It should be in config files. So that, we can replace the 3rd party application by changing in config files and very minimal change in code. This way it will require less testing and application will be ready very quickly with less effort and minimum impact.</li><li><b>Customizability</b>: It is very closely related with configurability. If application is highly configurable then it will be highly customizable. The vendor or client specific functionality in your application should be in plug-in-play mode. </li><li><b>Upgradeability</b>: It defines how easily your application's different components can be upgraded. Generally, we should use the built-in features of the technologies in our application so that we can upgrade our technology and all the built-in features will be upgraded automatically and easily. If we are using any 3rd party vendor's feature its difficult to upgrade as the upgraded feature may not support our application's underlying technology.</li><li><b>Integrity</b>: The application should be well integrated with other required business system. For example, your application should be integrated with analytic tool, dataware house etc. It should be capable to sharing the correct reports in terms of csv or excel with other business system. It should be easily integrated with any new incoming business component or vertical by applying some configuration changes and minimal code changes.</li></ol><div><b>Observability</b>: Observability is all about data exposure and easy access to information required to find issues when the communications fail, internal events do not occur as expected or events occur when they shouldn’t. The observability of your application can be enhanced by proper logging and error handling at every decision point or at the point of communication between different APIs and 3rd party API. We can create some Dashboard to display different metrics of our application. For these purpose, we can use tools like Grafana, Dyanatrace etc. We should also enable dashboard to visualize the logs like Kibana. In case of AWS, we can CloudWatch, Flowlogs for these purpose.</div><div><br /></div><div><b>Maintainability</b>: It refers the ease with which we can understand, repair and improve the code of our application. The more maintainable our application is the more easily we can fix bugs, enhance our application's functionalities and fixing the security issues. While performing maintenance activities we can make 4 types of changes to our application:</div><div><ol style="text-align: left;"><li><b>Corrective</b>: It is related with fixing the bugs and issues in our application. Application code should be well segregated in different files and folders according to its purpose so that required code fixes can be applied easily at the required place.</li><li><b>Adaptive</b>: Our application should be developed in such a way that it can easily accept new functionalities. If needed we can easily removed the deprecated or unwanted functionalities. It should be able to accept the updated libraries, DB, operating system etc.</li><li><b>Perfective</b>: This functionality includes the code changes to improve the application performance.</li><li><b>Preventive</b>: It is associated the required changes in application for enhancing security. Upgrading security is an ongoing and never end work. So, our application should always be ready to accept such security related changes.</li></ol></div><p></p><p><br /></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-1214531567722906672022-07-17T00:27:00.005-07:002022-07-21T03:53:09.914-07:00NFR : None Functional Requirement in Software Engineering or Application Development : Part 3: Application Security<p> In my previous posts, I have discussed about NFR definition, its requirement and 3 important NFRs like Scalability, Performance and Testability. Below are the links of all of my posts related with NFR:</p><p><b>Links</b>:</p><ul><li><a href="https://jksnu.blogspot.com/2022/03/inprogress.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 1</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 2: Scalability, Performance, Testability</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_17.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 3: Application Security</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_90.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 4: Extensibility, Observability, Maintainability</a></li></ul><p>Today, I am going to discuss about a very important NFR that is <b>Application Security</b>. Application security is an ongoing and never ending work. Every day, new security threats comes in cyber space and we along with Business team will have to keep eye open on these security threats and if it is going to impact our application, we should take preventive measures as soon as possible. Generally, application security is the responsibility of not only the Development team but it depends on all the participants of the application like: </p><p></p><ul style="text-align: left;"><li>Business team</li><li>Development team</li><li>End users</li><li>Hosting environment provider</li><li>Network provider</li></ul><div>Guidelines and conventions has been provided for each participants over internet and if every participants follows performs their roles very cautiously then only we can achieve the application security goal. Normally, it has been found that most of the application security issues comes either due to security loop holes present in application development side or from the End user side. The security issues from End user side can be minimized by increasing the awareness about secure use of the application among End users. This is completely separate set of efforts that application owner's Customer facing team should put to make their customer aware about secure use of application as much as possible. Here, I am not concentrating about customer awareness(although it is backbone of any application security). Here, I will discuss about the Development teams roles regarding implementation of security features in an application.</div><div><br /></div><div>I have developed several application in Node js and applied below security features. But, these are applicable to any public facing web application developed in any technology.</div><div><br /></div><div><ol style="text-align: left;"><li><b>CORS vulnerability</b>: It is abbreviated as Cross Origin Resource Sharing. Here, generally, our application exposes some APIs to share some data to some particular 3rd party API. So, we should keep a check that whether our exposed APIs are communicating with valid 3rd party APIs or some malicious services. This can be achieved by defining a White List array of 3rd party APIs or domain that will be allowed to interact with our application and rest all will be denied. There are several ways of implementing it. In Node Js, we can use CORS NPM package to implement it. If we are deploying our application in cloud like AWS, these cloud providers different security features like NACL, SecurityGroup</li><li><b>CSRF vulnerability</b>: It is abbreviated as Cross Site Request Forgery. Here, some malicious 3rd party API tries to send a malicious request to your application in such a way that your application assumes that the request is coming from a valid authenticated user. In Node JS application, this vulnerability can be restricted by using CSURF NPM package. Using this package we create a csurf token at Node JS side in a GET method at the very first time when request comes and then this token is sent to client in response body. Now, client should get this token from response body and set it in request header every time it is sending any request to Node JS. On Node JS side, we can define a middleware that will take this token from response header and try to match it with the token generated by Node JS. If it is matching then process the request and if it is not matching then return error response.</li><li><b>Request payload size limit</b>: Defining request payload max size limit on API side is very important. Generally, Node JS API reads the JSON request payload in synchronous mode and if the request payload size is very large then your application might hang or will become very slow. It should be checked before processing any request on nodejs side. If using Express framework, this size can be defined as app.use(express.json({ limit: 10 })). Here request payload size limit is defined as 10 BYTES. The default request payload size limit in EXPRESS is 100KB. I have created a post on it. Its URL is: <a href="http://jksnu.blogspot.com/2022/03/node-express-application-security-set.html" target="_blank"><b>Node JS application security</b></a></li><li><b>File upload size limit</b>: Define max file size limit if your application supports file upload. It can be defined by using Multer package like multer({limits: { fileSize: maxSize }}). Here, maxSize is in BYTES.</li><li><b>Input data sanitization</b>: Generally, the fraud like SQL injection and Javascript injections are done by input form data. This can be minimized by sanitizing the input data on API side before using it. Several NPM packages are available to sanitize input data like express-validator. In any database interaction, we should use parametrized query or any ORM library to minimize the chances of SQL injection. </li><li><b>Restrict XSS(Cross site scripting) attack</b>: This is very much similar to the security issue discussed just above under the heading "Input data sanitization". Here, along with above sanitization measures we can use XSS NPM package to read data from request object.</li><li>Use HTTPS instead of HTTP</li><li><b>Authorization</b>: We should follow the least privilege's mechanism with no previlege as Default. This can be implemented either by our own logic by creating tables where we can keep group of users with roles at group level and check the user's group and role before processing any request. Some available NPM package supporting it are @casl/ability, passport etc.</li><li><b>Authentication</b>: Use some well established 3rd party authentication mechanism instead of developing your own from scratch. The example of 3rd party authentication mechanism are like Social media authentication, Authentication using Azure or AWS authentication services. After successful authentication, we should create a JWT token by putting required data in token by encrypting it using Encryption algorithm provided by JWT. This token should be signed and sent to client. It should be properly validated before processing every request.</li><li>If any important data is coming from UI to API side it must be encrypted. The encryption key must be there in config file in UI. Same key must be used in API side to decrypt it.</li><li><b>Proper logging</b>: Proper logging should be maintained for each request keeping its user, IP information, timestamp, operation success or failure information etc.</li><li>OWASP top ten: The OWASP is an organization that provides the list of top 10 security vulnerabilities of application every year. The details can be found on https://owasp.org/Top10 These top 10 vulnerabilities must be taken care in our application at any cost. The top 10 security vulnerabilities for the year 2021 are as follows:</li></ol><div><ul style="text-align: left;"><li><b>A01:2021-Broken Access Control</b>: It occurs if we provide unwanted or extra privilege's to an user. It will be restricted by the ways discussed under the heading Authorization above with least privilege's.</li><li><b>A02:2021-Cryptographic Failures</b>: It occurs due to missing required encryption or due to encryption by deprecated or old libraries. It can be restricted by the ways discussed in point (10). The only difference is that the point(10) is talking about data in transit but this is talking about data in transit as well as data in rest. Here, we should use latest cryptographic libraries and always avoid deprecated library like md5 and SHA1. </li><li><b>A03 Injection</b>: It can be restricted by sanitizing request data and using parametrized queries in DB interaction as discussed in point(5 and 6)</li><li><b>A04:2021 – Insecure Design</b>: It is mainly related with Project Design. If project is not designed properly, even best coding practices can not rectify it. There are several tricks to minimize this flaws like using Limiting resource consumption by user or service, writing proper and sufficient unit test and integration test cases, using well established NPM packages. </li><li><b>A05 Security Misconfiguration</b>: Security is very vast and as a software engineer we have to constantly evaluate and upgrade our security feature in our application. We will have to keep verifying all the Cloud security features like SecurityGroup, NACL(Network Access Control Layer), IAM(Identity access management) policy etc. We should keep our 3rd party libs and NPM libs up to date. We should always try to move to the latest version of the programming language and DB if possible.</li><li><b>A06:2021 – Vulnerable and Outdated Components</b>: This can be mainly rectified by using the latest version of our programming language, DB, NPM package and any 3rd party APIs.</li><li><b>A07:2021 – Identification and Authentication Failures</b>: It mainly occurs if we are not able to keep our identity safe or using very week identity credentials like password or forget password question. To rectify it we should force user to make strong password and keep them properly encrypted in transit as well as in rest. Design your application to make it safe from Brute force attacks or automated attacks. If any crucial API is failing for 3 or more times for a particular user then block him for 30 min or slow down the processing for him. This kind of design will help in restricting automated attacks.</li><li><b>A08:2021 – Software and Data Integrity Failures</b>: It is mainly associated with the integrity failure or vulnerabilities related with the 3rd party APIs or NPM packages or CDN we are using in our application. So, if we are using any 3rd Party API, it should be interacted with HTTPS protocol along with proper validation of authentication and authorization tokens. We should use well established NPM package. </li><li><b>A09:2021 – Security Logging and Monitoring Failures</b>: It is associated with missing required logging and monitoring the logs regularly. It can be prevented by using proper logging in every request and setting up some Alarm or event like CloudWatch alarm in case of AWS to get notification via email or SMS about continuous failure of a particular API. In such cases we can validate logs to identify the cause of failure and to get the clue of any security attacks.</li><li><b>A10:2021 – Server-Side Request Forgery (SSRF)</b>: It mainly occurs by redirecting user to a different URL. It can be restricted by adding code for properly validating the domain name and port of the URL to ensure whether the domain, host and port are coming in our white list URL before redirecting user to that URL.</li></ul><div>Here, I have discussed the minimum required measures that we must take in our application development from security perspective. But, it is not a vast list of security features. There are several more tips and tricks that we can apply in our application. Application security implementation is an ongoing process and development team as well as Business team will have to keep working on it. Its not an one time activity.</div></div></div><div><br /></div><div>In my next post, I will discuss about few other NFR topics.</div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-83575320582250745682022-07-16T21:35:00.008-07:002022-07-21T03:52:56.858-07:00NFR : None Functional Requirement in Software Engineering or Application Development : Part 2: Scalability, Performance, Testability<p> In my previous post, I had discussed about below points like:</p><p></p><ol style="text-align: left;"><li>What is NFR</li><li>What information should be gathered by Development team from Business team with respect to NFR</li><li>Listed down few important NFRs</li></ol><div>Link of my NFR related posts:</div><div><ul><li><a href="https://jksnu.blogspot.com/2022/03/inprogress.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 1</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 2: Scalability, Performance, Testability</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_17.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 3: Application Security</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_90.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 4: Extensibility, Observability, Maintainability</a></li></ul></div><div><br /></div><div>In the continuation of my previous post about NFR, I am going to discuss about 3 NFR like <b>Scalability, Performance, Testability</b></div><div><br /></div><div><b>Scalability</b>:</div><div>Scalability is the measure of a system's ability to increase or decrease in performance and cost in response to changes in application and system processing demands. There are 3 important types scalability:</div><div><ol style="text-align: left;"><li>Horizontal Scaling: New server is added instead of upgrading the existing server.</li><li>Vertical Scaling: Existing server is upgraded based by increasing CPU, RAM, Processor</li><li>Diagonal Scaling: Here, both horizontal and vertical scaling are done.</li></ol><div>Below are few of the ways of implementing scalability in your application:</div></div><div><ol style="text-align: left;"><li>Setup an alarm like CloudWatch Alarm in AWS that triggers an alarm or event when CPU utilization reaches a thresh hold value</li><li>Setup an autoscaling by using cloud autoscale functionality or Kubernates</li><li>Load Balancing: Define load balancer to equally distribute load on several servers</li><li>Enable clustering if your application technology supports. E.g. if your application is using NodeJs for API creation, we can easily enable clustering as discussed in my below posts like:</li></ol><div> <a href="http://jksnu.blogspot.com/2022/02/cluster-in-node-js-application.html" target="_blank">Cluster in Node Js application</a><br /></div><div> <a href="http://jksnu.blogspot.com/2022/02/cluster-management-in-node-js.html" target="_blank">Cluster management in Node Js application by PM2 Lib</a><br /></div><div><br /></div><div><b>Performance</b>:</div></div><div>It represents the application's throughput time. Lower the throughput time, higher is the performance. Below are few of the ways to enhance your application's performance:</div><div><ol style="text-align: left;"><li>Setup proper load balancer</li><li>Use auto-scaling to enhance performance based on CloudWatch alarm</li><li>Setup caching like Reddish or memcache</li><li>Flush the cache data at certain intervals based on frequency of write operation in the application and how early to present the latest data to customers</li><li>Always use Asynchronous operations where synchronous behavior is not required</li><li>Follow all coding conventions and guidelines</li><li>Follow all best practices of DB like SQL query optimization, using Views, Indexes, Stored Procedures and SQL functions where ever required.</li><li>Use some code validation tools like Sonar or Eslint</li><li>Try to use latest stable and suitable version of libs like NPM packages</li></ol><div><b>Testability</b>:</div></div><div>In software, testability refers to the degree that any module, requirements, subsystem or other component of your architecture can be verified as satisfactory or not. High testability means it is easy to find and isolate faults as part of your team's regular testing process. Our application must have following criteria to be more testable:</div><div><ol style="text-align: left;"><li>Controllability: The degree to which it is possible to control the state of the component under test (CUT) as required for testing.</li><li>Observability: The degree to which it is possible to observe (intermediate and final) test results.</li><li>Isolateability: The degree to which the component under test (CUT) can be tested in isolation.</li><li>Separation of concerns: The degree to which the component under test has a single, well defined responsibility</li><li>Understandability: The degree to which the component under test is documented or self-explaining.</li><li>Automatability: The degree to which it is possible to automate testing of the component under test.</li><li>Heterogeneity: The degree to which the use of diverse technologies requires to use diverse test methods and tools in parallel.</li></ol><div>In my next post, I will discuss about NFR related with Application security.</div></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-61181412908457862112022-03-04T05:50:00.004-08:002022-07-21T03:51:27.413-07:00NFR : None Functional Requirement in Software Engineering or Application Development : Part 1<p>Here is the link of all of my posts on topic NFR:</p><p>Links:</p><p></p><ul style="text-align: left;"><li><a href="https://jksnu.blogspot.com/2022/03/inprogress.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 1</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 2: Scalability, Performance, Testability</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_17.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 3: Application Security</a></li><li><a href="https://jksnu.blogspot.com/2022/07/nfr-none-functional-requirement-in_90.html" target="_blank">NFR : None Functional Requirement in Software Engineering or Application Development : Part 4: Extensibility, Observability, Maintainability</a></li></ul><p></p><p>NFR(None Functional Requirement) is abbreviated as Non Functional Requirement. In any application development or maintenance work, the NFRs are least discussed but most required activities. Generally, the NFRs of an application are not discussed by Business team. Its Developer's team's responsibility to discuss all the NFRs with business teams and try to understand and create a document of the discussion regarding NFRs implementation with Business team. There may be several discussion points regarding NFRs with Business team like:</p><p></p><ol style="text-align: left;"><li>Expected minimum or maximum load on application</li><li>What is the pick time and off pick time of the application usage</li><li>Is the application public facing or only client's employee facing</li><li>Application downtime required for build deployment or any upgradation</li><li>Response throughput in terms of millisecond required for a feature</li><li>Negotiation in business requirement to achieve the required throughput.</li><li>Whether the application's feature is read extensive or write extensive to decide how frequently data should be flushed in caching if caching is implemented</li><li>Whether customer requires latest data as soon as it is updated in system to decide whether to implement caching</li><li>Cost of any 3rd party API that Business can bear.</li><li>Cost of skilled resources that Business should bear till the time the application becomes stable(Should be discussed more by Management team)</li><li>Tradeoff analysis and discuss with Business team about conflict between different NFRs.</li></ol><div>There are several types of Non Functional Requirements. Few important NFRs are as follows:</div><div><ol style="text-align: left;"><li>Scalability</li><li>Performance</li><li>Testability</li><li>Security</li><li>Extensibility</li><li>Observability</li><li>Maintainability</li></ol><div>The actual list of NFRs are very vast. Here, I have included only few that are mostly required to be considered in Application development.</div><div><br /></div></div><div>In my next blog, I will discuss about the above mentioned NFRs in short.</div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-28988162310777230972022-03-04T05:37:00.006-08:002022-03-04T05:46:56.992-08:00Node Express application Security: Set Request Size Limit for JSON data and file uploading<p> Here, we are going to discuss about securing our Node Express application if any unwanted user tries to impact performance of our application by sending a very huge input JSON data or by uploading a very large file. </p><p>Generally, Express allows 100KB JSON data by default. If we try to input more than 100KB JSON data, it will return 413 error code. In case of file uploading by using Multer package, there is not any defult file size limit. So, in can case of file uploading, we should apply some file size limit in Node Js code. Lets discuss about JSON input data and file uploading separately.</p><p><b>JSON input data: </b>As told above, Express allows by default 100KB JSON data which is pretty good in most of the scenarios. If in some scenarios, we need to send JSON data of size more than 100KB, then we have to increase the default JSON data size limit. We can achieve it by using the below two middlewares.</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">json</span>({<span style="color: #9cdcfe;">limit</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'10mb'</span>, <span style="color: #9cdcfe;">extended</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>}));</div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">urlencoded</span>({<span style="color: #9cdcfe;">limit</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'10mb'</span>, <span style="color: #9cdcfe;">extended</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>})); </div></div><p>Now, this express application will allow us to send JSON data of up to 10mb size.</p><p><b>File upload size limit: </b>Generally, we use Multer package to upload incoming files. Here, we can restrict the security threat by providing the max file size limit in multer. It can be done in the following way.</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">multer</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'multer'</span>);</div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div style="line-height: 19px;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">upload</span> = <span style="color: #dcdcaa;">multer</span>({<span style="color: #9cdcfe;">limits</span><span style="color: #9cdcfe;">:</span> { <span style="color: #9cdcfe;">fileSize</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">1024</span> * <span style="color: #b5cea8;">1024</span> * <span style="color: #b5cea8;">150</span> }}); <span style="color: #6a9955;">//150Mb</span></div><div style="line-height: 19px;"><span style="color: #6a9955;"><br /></span></div></div><p>Here, we are defining the maximum allowed file size limit as 150mb. This size applies for both single file upload and multiple file upload. So, if we are uploading a single file of size say 200mb or two files together with each file having size about 100mb that is 100+100=200mb then, in both the cases, the file size limit is exceeding the defined file size that is 150mb. So, in these cases, it will give error with 500 status code with error message as "File too large".</p><p>I have created below two routes for handling single file upload and multiple file upload.</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/singleFileUpload'</span>, <span style="color: #4fc1ff;">upload</span>.<span style="color: #dcdcaa;">single</span>(<span style="color: #ce9178;">"file"</span>), (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jsonData</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span></div><div> });</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/multipleFileUpload'</span>, <span style="color: #4fc1ff;">upload</span>.<span style="color: #dcdcaa;">array</span>(<span style="color: #ce9178;">"file"</span>), (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jsonData</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span></div><div> });</div><div>});</div></div><p>Here, I am giving my code that will help in reproducing the outcome of above discussion.</p><p>package.json</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"node_test"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span>,</div><div> <span style="color: #9cdcfe;">"start"</span>: <span style="color: #ce9178;">"nodemon ./app"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"express"</span>: <span style="color: #ce9178;">"^4.17.3"</span>,</div><div> <span style="color: #9cdcfe;">"multer"</span>: <span style="color: #ce9178;">"^1.4.4"</span>,</div><div> <span style="color: #9cdcfe;">"nodemon"</span>: <span style="color: #ce9178;">"^2.0.15"</span></div><div> }</div><div>}</div><br /></div><p>app.js</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">multer</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'multer'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">upload</span> = <span style="color: #dcdcaa;">multer</span>({<span style="color: #9cdcfe;">limits</span><span style="color: #9cdcfe;">:</span> { <span style="color: #9cdcfe;">fileSize</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">1024</span> * <span style="color: #b5cea8;">1024</span> * <span style="color: #b5cea8;">150</span> }}); <span style="color: #6a9955;">//50Mb</span></div><br /><div><span style="color: #6a9955;">//middleware</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">json</span>({<span style="color: #9cdcfe;">limit</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'10mb'</span>, <span style="color: #9cdcfe;">extended</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>}));</div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">urlencoded</span>({<span style="color: #9cdcfe;">limit</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'10mb'</span>, <span style="color: #9cdcfe;">extended</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>})); </div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">3000</span>;</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span></div><div> });</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/bigJson'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jsonData</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span>,</div><div> <span style="color: #ce9178;">"jsonData"</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">jsonData</span></div><div> });</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/singleFileUpload'</span>, <span style="color: #4fc1ff;">upload</span>.<span style="color: #dcdcaa;">single</span>(<span style="color: #ce9178;">"file"</span>), (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jsonData</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span></div><div> });</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/multipleFileUpload'</span>, <span style="color: #4fc1ff;">upload</span>.<span style="color: #dcdcaa;">array</span>(<span style="color: #ce9178;">"file"</span>), (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jsonData</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>;</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">status</span>(<span style="color: #b5cea8;">200</span>).<span style="color: #dcdcaa;">json</span>({</div><div> <span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"success"</span></div><div> });</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"Server has started"</span>);</div><div>});</div></div><p><br /></p><p><br /></p><p><br /></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-88213697142513561872022-02-09T02:29:00.002-08:002022-02-09T02:29:42.878-08:00Publish message to AWS SQS queue by NodeJs<p> Here, we will discuss about how to publish messages to AWS SQS queue by NodeJs. To implement it, we have to follow below steps:</p><p></p><ol style="text-align: left;"><li>Create an user in AWS and note down its access key Id and secret access key</li><li>Create a SQS queue and note down its url</li><li>Save user credential in a shared file in your local</li><li>Install aws-sdk library</li><li>Create a simple node express application with route for publishing message to SQS</li></ol><div>Now, lets discuss each steps one by one.</div><div>1) Create an user in AWS and note down its access key Id and secret access key: <span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;">I have discussed in detail about this steps in my previous blog under the heading "File handling in AWS S3 by NodeJs". I am giving the link below. Please go through it.</span></div><p></p><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="font-size: 13.2px;"><b><a href="https://jksnu.blogspot.com/2022/02/file-handling-in-aws-s3-by-node-js.html#:~:text=1)-,Create%20an%20user%20in%20IAM%20service%20in%20AWS,-%3A%20We%20should%20go" style="color: #2288bb; text-decoration-line: none;" target="_blank">Create an user in IAM service in AWS</a></b></span></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="font-size: 13.2px;"><br /></span></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="font-size: 13.2px;">2) </span><span style="background-color: transparent;">Create a SQS queue and note down its url: First open AWS console and go to the SQS service and click on the "Create Queue" button. Now, add the Queue name and keep other things with default value. Further click on "Create Queue" button at the bottom.</span></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><span style="background-color: transparent;"><br /></span></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgWkqOlQG4gVdBkhVjg9Vb3lLN_SzxBh2obBCZU6kirqM6iPDzOR4YA_6hkZ8RlpIE_I0VEOCweCC1xoSHRUMEwpdAPSCzB6xKLGqPHUm1zOBZ1wz2sonsdZ0mP2MZ-oGL9gwN1ufMjuKa61TvH6Eq6CdpHNPg9Y42QUgn1rR5LYx4G8oA77SrE2qub=s992" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="518" data-original-width="992" height="286" src="https://blogger.googleusercontent.com/img/a/AVvXsEgWkqOlQG4gVdBkhVjg9Vb3lLN_SzxBh2obBCZU6kirqM6iPDzOR4YA_6hkZ8RlpIE_I0VEOCweCC1xoSHRUMEwpdAPSCzB6xKLGqPHUm1zOBZ1wz2sonsdZ0mP2MZ-oGL9gwN1ufMjuKa61TvH6Eq6CdpHNPg9Y42QUgn1rR5LYx4G8oA77SrE2qub=w548-h286" width="548" /></a></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><br /></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhdQTJrssdVzJ7HnqxTO1170RGMYkQyJ-w-RfQ9cOz28yZuY6erlcj1BV_MoQH3RI7hjPxz_MsTLJaz4Sun1I5j89aYtaYkDgFen1xES5NmRCqxFy4R0icVB1pl9ORIqjqY4eC_yki26k5sUPQYHfkAT4oOLE4FS-MJnG5rrbuIlwdjzZWM0ccAY5Lj=s1226" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="277" data-original-width="1226" height="136" src="https://blogger.googleusercontent.com/img/a/AVvXsEhdQTJrssdVzJ7HnqxTO1170RGMYkQyJ-w-RfQ9cOz28yZuY6erlcj1BV_MoQH3RI7hjPxz_MsTLJaz4Sun1I5j89aYtaYkDgFen1xES5NmRCqxFy4R0icVB1pl9ORIqjqY4eC_yki26k5sUPQYHfkAT4oOLE4FS-MJnG5rrbuIlwdjzZWM0ccAY5Lj=w503-h136" width="503" /></a></div><br /><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><br /></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;">3) <span style="color: black; font-family: "Times New Roman"; font-size: medium;">Save user credential in a shared file in your local: </span><span style="font-size: 13.2px;">In this steps, we should save the user credential so that node js application can read it while establishing connection with AWS. We can refer below AWS documentation for this step:</span></div><div style="font-size: 13.2px;"><br /></div><div style="font-size: 13.2px;"><a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html" style="color: #2288bb; text-decoration-line: none;" target="_blank">Save User Credentials</a></div><div style="font-size: 13.2px;"><br /></div><div style="font-size: 13.2px;">5) <span style="color: black; font-family: "Times New Roman"; font-size: medium;">Install aws-sdk library: Create a simple node js application and </span><span style="font-size: 13.2px;">Use the command like npm install aws-sdk</span></div><div style="font-size: 13.2px;"><span style="font-size: 13.2px;"><br /></span></div><div style="font-size: 13.2px;"><span style="font-size: 13.2px;">6) </span><span style="color: black; font-family: "Times New Roman"; font-size: medium;">Create a simple node express application with route for publishing message to SQS: Here, we are creating a very simple node express application along with two routes:</span></div><div style="font-size: 13.2px;"><span style="color: black; font-family: "Times New Roman"; font-size: medium;">a) "</span><span style="background-color: #1e1e1e; color: #ce9178; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">/addMessage</span><span style="color: black; font-family: "Times New Roman"; font-size: medium;">": This is being used to add messages in SQS queue.</span></div><div style="font-size: 13.2px;"><span style="color: black; font-family: "Times New Roman"; font-size: medium;">b) "</span><span style="background-color: #1e1e1e; color: #ce9178; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">/getMessage</span><span style="color: black; font-family: "Times New Roman"; font-size: medium;">": This is being used to get messages from the SQS queue.</span></div><div style="font-size: 13.2px;"><span style="color: black; font-family: "Times New Roman"; font-size: medium;">I am providing here the code for your reference:</span></div><div style="font-size: 13.2px;"><span style="color: black; font-family: "Times New Roman"; font-size: medium;"><br /></span></div><div style="font-size: 13.2px;"><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">dotenv</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'dotenv'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">path</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'path'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">AWS</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'aws-sdk'</span>);</div><br /><div><span style="color: #4fc1ff;">AWS</span>.<span style="color: #9cdcfe;">config</span>.<span style="color: #dcdcaa;">update</span>({<span style="color: #9cdcfe;">region</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'us-east-1'</span>});</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">sqs</span> = <span style="color: #569cd6;">new</span> <span style="color: #4fc1ff;">AWS</span>.<span style="color: #4ec9b0;">SQS</span>();</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">json</span>());</div><br /><div><span style="color: #9cdcfe;">dotenv</span>.<span style="color: #dcdcaa;">config</span>({</div><div> <span style="color: #9cdcfe;">path</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'./.env'</span>)</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #b5cea8;">8000</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Listening port 8000`</span>);</div><div>});</div><br /><div><span style="color: #6a9955;">//Add a message to SQS</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/addMessage'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">params</span> = {</div><div> <span style="color: #9cdcfe;">MessageBody</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">MessageBody</span>,</div><div> <span style="color: #9cdcfe;">QueueUrl</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">SQS_URL</span></div><div> };</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">sqs</span>.<span style="color: #dcdcaa;">sendMessage</span>(<span style="color: #4fc1ff;">params</span>).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>)</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div>});</div><br /><div><span style="color: #6a9955;">//Receive a message to SQS</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/getMessage'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">params</span> = {</div><div> <span style="color: #9cdcfe;">QueueUrl</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">SQS_URL</span></div><div> };</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">sqs</span>.<span style="color: #dcdcaa;">receiveMessage</span>(<span style="color: #4fc1ff;">params</span>).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>)</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div>});</div><br /><br /><br /></div></div><br /><span style="background-color: transparent;"><br /></span></div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-2288104928648927312022-02-07T23:01:00.003-08:002022-02-08T02:24:05.184-08:00Publish and subscribe to AWS SNS topic through NodeJS<p> Today, we are going to discuss how to publish a message to AWS SNS topic and how to subscribe to the SNS topic so that the subscriber gets the messages published to the SNS topic. We should follow the below steps:</p><p></p><ol style="text-align: left;"><li> Create an user and user group and note down its accessKeyId and secretAccessKey</li><li>Assign this user the required permission for SNS handling</li><li>Create a topic and note down its ARN</li><li>Install aws-sdk libraray</li><li>Save user credential in a shared file in your local</li><li>Create a simple node express application and create routes for publish and subscribe</li></ol><div>Now, lets discuss about each steps here:</div><div>1) Create an user and user group and note down its accessKeyId and secretAccessKey: I have discussed in detail about this steps in my previous blog under the heading "File handling in AWS S3 by NodeJs". I am giving the link below. Please go through it.</div><div><br /></div><div><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;"><b><a href="https://jksnu.blogspot.com/2022/02/file-handling-in-aws-s3-by-node-js.html#:~:text=1)-,Create%20an%20user%20in%20IAM%20service%20in%20AWS,-%3A%20We%20should%20go" target="_blank">Create an user in IAM service in AWS</a></b></span></div><div><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;"><br /></span></div><div><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;">2) </span>Assign this user the required permission for SNS handling: Here, we can create a custom policy for SNS handling and then assign it to the group to which the user belongs or we can assign a default SNS policy already provided by AWS. Here, I am going to assign default SNS policy.</div><div>First, go to the IAM service in AWS console and then click on your User name to whom you want to provide permission. </div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwFB5xm-NI9EUOlQXqsmjKrFG1O36HrvNs0spPacCprh2khjRYk6APf6Mabeqzeg1k2l7piw0Hv6zRI_I4WUMN4-l4KRWTRvEh6WqgC3uI4HY6h8pDcN_D-951L2FaFj8RHnVJaAAQgXQFoWGNnjAT1bp4HjAsHa9dpP81EgDYC9Wxp3q7WEQW1EaZ/s1075/Capture.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="464" data-original-width="1075" height="280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwFB5xm-NI9EUOlQXqsmjKrFG1O36HrvNs0spPacCprh2khjRYk6APf6Mabeqzeg1k2l7piw0Hv6zRI_I4WUMN4-l4KRWTRvEh6WqgC3uI4HY6h8pDcN_D-951L2FaFj8RHnVJaAAQgXQFoWGNnjAT1bp4HjAsHa9dpP81EgDYC9Wxp3q7WEQW1EaZ/w572-h280/Capture.PNG" width="572" /></a></div><br /><div>Now, click on Add permissions button and then click on "Attach existing policies directly". Now, search for SNS and select "AmazonSNSFullAccess". </div><div><b>Important Note:</b> This policy selection is only for demo purpose. In real time, we should provide the least required access for any service to an user. Always avoid providing the full access.</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFddLSJFG8-Fnw-PCFocs81xNtZraEsKOAKR1GYB1ICvn0dLOshlApveQsZEJ_AvgVJUqspBlmf9N8Xq_0ElOdDF7cQQuPeHNcSIUSZcqIAcdhBioVPYQ4pJ3LRWT_bxdUVnwl5dEVI0yS8u04ne9Lar6DiK1km9UypWp6zrb-y_D93AV9qNYIefBK/s1142/Capture2.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="560" data-original-width="1142" height="287" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFddLSJFG8-Fnw-PCFocs81xNtZraEsKOAKR1GYB1ICvn0dLOshlApveQsZEJ_AvgVJUqspBlmf9N8Xq_0ElOdDF7cQQuPeHNcSIUSZcqIAcdhBioVPYQ4pJ3LRWT_bxdUVnwl5dEVI0yS8u04ne9Lar6DiK1km9UypWp6zrb-y_D93AV9qNYIefBK/w532-h287/Capture2.PNG" width="532" /></a></div><br /><div>3) Create a topic and note down its ARN: Now, go to the SNS service in AWS console and click on the Topics tab present in the left side. Further, click on the Create topic button. In the create topic window, select "Standard" radio button and add your Topic name and display name. You can keep rest of the fields as default.</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjZzTvXQbwpBMFnslMfvqtd5AbL0rMgXH3Vu0PHvQB12gTsf13DpM-UtiKnNDoEMA6hLb64TPx7tRJDkzhsIBwRGTGj-3banVsPWn60vSdGLpEq6TUZWBdnj5Dp4_Eof_l3_0jL0L6M5y0QoN7lwxN7I3noxiN7Cq7icVOfhTFJBlNqpSbgr1_JUy_/s814/Capture3.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="519" data-original-width="814" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjZzTvXQbwpBMFnslMfvqtd5AbL0rMgXH3Vu0PHvQB12gTsf13DpM-UtiKnNDoEMA6hLb64TPx7tRJDkzhsIBwRGTGj-3banVsPWn60vSdGLpEq6TUZWBdnj5Dp4_Eof_l3_0jL0L6M5y0QoN7lwxN7I3noxiN7Cq7icVOfhTFJBlNqpSbgr1_JUy_/w564-h360/Capture3.PNG" width="564" /></a></div><br /><div>Now, your topic will be created. You should note down the ARN of the topic. It is going to be used in NodeJS code.</div><div><br /></div><div>4) Install aws-sdk libraray: Use the command like npm install aws-sdk</div><div><br /></div><div>5) Save user credential in a shared file in your local: <span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;">In this steps, we should save the user credential so that node js application can read it while establishing connection with AWS. We can refer below AWS documentation for this step:</span></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><br /></div><div style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html" style="color: #2288bb; text-decoration-line: none;" target="_blank">Save User Credentials</a></div><div><br /></div><div>6) Create a simple node express application and create routes for publish and subscribe: Here, we should create a node js and express application and expose two routes. </div><div>One is used to subscribe the email id to the SNS topic represented by the ARN of the topic in the code. Once, the route is triggered, it will trigger SNS service to subscribe the given email id to the SNS topic. You will get a confirmation email on this email id from AWS. This email will have a link and you will have to click on the link to complete the subscription.</div><div>Second route is used to publish message to the SNS topic and further SNS topic will forward the message to all the subscribed emails. Here, we have to provide the Subject and Body as a part of message. </div><div>I am giving here the code for your reference:</div><div><br /></div><div style="background-color: #1e1e1e; line-height: 19px;"><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">dotenv</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'dotenv'</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">path</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'path'</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">AWS</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'aws-sdk'</span>);</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">AWS</span>.<span style="color: #9cdcfe;">config</span>.<span style="color: #dcdcaa;">update</span>({<span style="color: #9cdcfe;">region</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'us-east-1'</span>});</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">sns</span> = <span style="color: #569cd6;">new</span> <span style="color: #4fc1ff;">AWS</span>.<span style="color: #4ec9b0;">SNS</span>();</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">use</span>(<span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">json</span>());</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #9cdcfe;">dotenv</span>.<span style="color: #dcdcaa;">config</span>({</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">path</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'./.env'</span>)</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">});</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #b5cea8;">8000</span>, () <span style="color: #569cd6;">=></span> {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Listening port 8000`</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">});</div><br /><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #6a9955;">//Get sns details</span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/mysns'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>({<span style="color: #ce9178;">"status"</span><span style="color: #9cdcfe;">:</span><span style="color: #ce9178;">"Ok"</span>, <span style="color: #9cdcfe;">sns</span>}));</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #6a9955;">//Subscribe SNS topic</span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/subscribe'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #c586c0;">try</span> {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">params</span> = {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">Protocol</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Email'</span>,</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">TopicArn</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">TOPIC_ARN</span>,</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">Endpoint</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">email</span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> };</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">sns</span>.<span style="color: #dcdcaa;">subscribe</span>(<span style="color: #4fc1ff;">params</span>).<span style="color: #dcdcaa;">promise</span>();</div><div><span style="color: #d4d4d4; font-family: Consolas, Courier New, monospace;"><span style="font-size: 14px; white-space: pre;"> </span></span><span style="color: #c586c0; font-family: Consolas, Courier New, monospace;"><span style="font-size: 14px; white-space: pre;">res.send(result);</span></span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> } </div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">});</div><br /><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #6a9955;">//Publish to SNS topic</span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/publish'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #c586c0;">try</span> {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">params</span> = {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">Subject</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">subject</span>,</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">TopicArn</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">TOPIC_ARN</span>,</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">Message</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">message</span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> };</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">sns</span>.<span style="color: #dcdcaa;">publish</span>(<span style="color: #4fc1ff;">params</span>).<span style="color: #dcdcaa;">promise</span>();</div><div><span style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> </span><span style="color: #c586c0; font-family: Consolas, Courier New, monospace;"><span style="font-size: 14px; white-space: pre;">res.send(result);</span></span></div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> } </div><div style="color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">});</div><br /><br /><br /></div><div> </div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-61465476946206493652022-02-06T22:51:00.000-08:002022-02-06T22:51:02.423-08:00File handling in AWS S3 by Node Js<p> Today, I am going to discuss about file handling operations like upload a file, get list of all the files, delete a file and download files from S3 bucket in AWS. To perform all these operation, we should follow the below steps:</p><p></p><ol style="text-align: left;"><li>Create an user in IAM service in AWS</li><li>Create a bucket in S3 service in AWS</li><li>Save user credential in a shared file in your local</li><li>Create a NodeJS application</li><li>Install aws-sdk package</li><li>Create routes for handling files</li></ol><div>Now, lets discuss about each steps here.</div><div>1) Create an user in IAM service in AWS: We should go to the IAM service in AWS and click on "Add users button".</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgKtTID_OSCPSdvw4twKC6UKfzkQ_qeobUSRSE_i0whYPVIYMpCv3M62U-AaO-V5zpKXyjMKttBb8CPfBLHW7hw-UlrKyflkzMwTtoi-kCQXJENpUkx_8M2pLLKqn3r4LqVCFHscbXYwJFCsX7k5boijGcGUhHwzgi1m_46bTbHPcrAWsdwBdaEMQXo=s1321" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="263" data-original-width="1321" height="111" src="https://blogger.googleusercontent.com/img/a/AVvXsEgKtTID_OSCPSdvw4twKC6UKfzkQ_qeobUSRSE_i0whYPVIYMpCv3M62U-AaO-V5zpKXyjMKttBb8CPfBLHW7hw-UlrKyflkzMwTtoi-kCQXJENpUkx_8M2pLLKqn3r4LqVCFHscbXYwJFCsX7k5boijGcGUhHwzgi1m_46bTbHPcrAWsdwBdaEMQXo=w557-h111" width="557" /></a></div><br /><div>Add the user name and select the check box for "Access key - Programmatic access" and then click on "Next: Permissions"</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEixcoSogs8CVTC_duFMUTEti_pXh--P2pmhJ8Z5_PlvG9nsH1HEQkRQFDeLw9wNREDBuBenfeAuzMlRapdyjBjKFWz86PQiPEpd2Y2CVN9aJsDJXs7T9AmTTwV9iObqKQdCdbHssQ9HqWKaVzJ6FiFCJDsKTsJrA22K9FqbCIpUAcDOeh5rvUIEU8OL=s982" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="570" data-original-width="982" height="303" src="https://blogger.googleusercontent.com/img/a/AVvXsEixcoSogs8CVTC_duFMUTEti_pXh--P2pmhJ8Z5_PlvG9nsH1HEQkRQFDeLw9wNREDBuBenfeAuzMlRapdyjBjKFWz86PQiPEpd2Y2CVN9aJsDJXs7T9AmTTwV9iObqKQdCdbHssQ9HqWKaVzJ6FiFCJDsKTsJrA22K9FqbCIpUAcDOeh5rvUIEU8OL=w522-h303" width="522" /></a></div><br /><div>Click on the "Create group" button and then add a group name and then click on Create group button.</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhj0ekaFXjhtQhowBBaVVhY4uvFjnI9ql1Wv2uAQoicNU6nXZ_fNKE66UakmeiLn6oUB27GCHYB_KmyFlsUWR-J25Lu610mLeYxCBvxbmNvHSkbSAdgZrQBWxfWQChPflQAC7xhAzJP6ro2gm9Bitmq7jUNICp0_j31_iZgYaKsGvw1Ti01G4ZbSEt_=s1126" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="563" data-original-width="1126" height="270" src="https://blogger.googleusercontent.com/img/a/AVvXsEhj0ekaFXjhtQhowBBaVVhY4uvFjnI9ql1Wv2uAQoicNU6nXZ_fNKE66UakmeiLn6oUB27GCHYB_KmyFlsUWR-J25Lu610mLeYxCBvxbmNvHSkbSAdgZrQBWxfWQChPflQAC7xhAzJP6ro2gm9Bitmq7jUNICp0_j31_iZgYaKsGvw1Ti01G4ZbSEt_=w540-h270" width="540" /></a></div><br /><div>Now, on group list page, click on your newly created group and then click on Permissions tab. Now, Add Permissions dropdown box and click on attach Policy.</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgVk3IkJgTqN1cJPFeZ4O0ND4-Y1VwBpLOe8DuNgo3yYxomrF_2DX1I140muSwt2AEBlu8SrPR4eGDLsnP2qRG5ktatTT5mjdQzd1Z0EfYpDh1ogT_Vnr85ZgOBG7kXeypwzTZgPGIdvpAK7_PBDn_dQgD7M3CDgbwXWvHjb1wFvLiBW3efjMg-yeh5=s1255" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="495" data-original-width="1255" height="222" src="https://blogger.googleusercontent.com/img/a/AVvXsEgVk3IkJgTqN1cJPFeZ4O0ND4-Y1VwBpLOe8DuNgo3yYxomrF_2DX1I140muSwt2AEBlu8SrPR4eGDLsnP2qRG5ktatTT5mjdQzd1Z0EfYpDh1ogT_Vnr85ZgOBG7kXeypwzTZgPGIdvpAK7_PBDn_dQgD7M3CDgbwXWvHjb1wFvLiBW3efjMg-yeh5=w523-h222" width="523" /></a></div><br /><div>Now, click on Create Policy and select service as S3. Further, select check boxes of Read, List and Write</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEiwD_JmJY9byuOtNpIjpikGAg-LzosltRAimavYmrxzAlsITGWgWne0JOUhn_qsBYCFKwtfgDo5LRnPKIxuMblMaX3T1WqYg2BFGsjNTyNDrsNe49v86TcHlseXA6MrOErmt22csQpOC9s5DYgSgUmdnpZAPJXNlRPxwHOvO2yixDYTJ6Q_LMXPwKhc=s1162" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="518" data-original-width="1162" height="278" src="https://blogger.googleusercontent.com/img/a/AVvXsEiwD_JmJY9byuOtNpIjpikGAg-LzosltRAimavYmrxzAlsITGWgWne0JOUhn_qsBYCFKwtfgDo5LRnPKIxuMblMaX3T1WqYg2BFGsjNTyNDrsNe49v86TcHlseXA6MrOErmt22csQpOC9s5DYgSgUmdnpZAPJXNlRPxwHOvO2yixDYTJ6Q_LMXPwKhc=w553-h278" width="553" /></a></div><br /><div><br /></div><div>So, up to here, your user and required group and policy is created. Now, note down the user access key id and secret access key. These two keys are going to be used in NodeJS programming.</div><div><br /></div><div>2) Create a bucket in S3 service in AWS: Now, we should go to the S3 service of AWS and create a bucket with unique name in AWS.</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEj4QZOpR_2j0QcoWlmIuFOwqnMhFhg9P_7917kb1wJWAri6bhG8oqf_ovZZpSpn7F88Rm-6nEHhttJ2hMiEdsdw-98b6ELbphD6ey8mBucw6EOhUDlbR1okb_4FL1GcE5w_Nd3L5V1IQgM-g2MN0riabkPfTHkILQjZHvN-d41PZU7zRbD57Ris_f9e=s797" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="550" data-original-width="797" height="368" src="https://blogger.googleusercontent.com/img/a/AVvXsEj4QZOpR_2j0QcoWlmIuFOwqnMhFhg9P_7917kb1wJWAri6bhG8oqf_ovZZpSpn7F88Rm-6nEHhttJ2hMiEdsdw-98b6ELbphD6ey8mBucw6EOhUDlbR1okb_4FL1GcE5w_Nd3L5V1IQgM-g2MN0riabkPfTHkILQjZHvN-d41PZU7zRbD57Ris_f9e=w533-h368" width="533" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgC6cSf5gMZLFab6F4l-yWszKe-ZWp2F-CSZ9t06P-FCG8qsg6eiaA_KoM3gFw1nzIgJH5gaAlDV4vzw4j0ZhxJyPinZFxmq0KaKnyg55-3e-_62nOL6-pYdZXaGiyUS0kSMLKmUI4WKAAmz0y-T_-vaQrhBIOSWJNfAbEwF5N4DLRsx5E_1J480JLH=s773" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="513" data-original-width="773" height="340" src="https://blogger.googleusercontent.com/img/a/AVvXsEgC6cSf5gMZLFab6F4l-yWszKe-ZWp2F-CSZ9t06P-FCG8qsg6eiaA_KoM3gFw1nzIgJH5gaAlDV4vzw4j0ZhxJyPinZFxmq0KaKnyg55-3e-_62nOL6-pYdZXaGiyUS0kSMLKmUI4WKAAmz0y-T_-vaQrhBIOSWJNfAbEwF5N4DLRsx5E_1J480JLH=w514-h340" width="514" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEi9ux9Hvs093avmsS9fUjpGICi0IjnX22aDsLUoRce1Ysf__SFqB8tGfJom81dMRj7I-Eqwn9CX-Qt5SpfKbfHfjswyPrmnUyefDdeKvtnnE9S78timh0mJBR1H7g0dvpMNzu_v1MYXer-YDcMnIp_Fu7xTKj-my6ds4YnPUlN-2SRoFa0FB96-8XJB=s788" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="588" data-original-width="788" height="382" src="https://blogger.googleusercontent.com/img/a/AVvXsEi9ux9Hvs093avmsS9fUjpGICi0IjnX22aDsLUoRce1Ysf__SFqB8tGfJom81dMRj7I-Eqwn9CX-Qt5SpfKbfHfjswyPrmnUyefDdeKvtnnE9S78timh0mJBR1H7g0dvpMNzu_v1MYXer-YDcMnIp_Fu7xTKj-my6ds4YnPUlN-2SRoFa0FB96-8XJB=w511-h382" width="511" /></a></div><br /><div><br /></div><div>3) Save user credential in a shared file in your local: In this steps, we should save the user credential so that node js application can read it while establishing connection with AWS. We can refer below AWS documentation for this step:</div><div><br /></div><div><a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html" target="_blank">Save User Credentials</a></div><div><br /></div><div>4) Create a NodeJS application: Here, create a very simple node js application with Express. Code will be given in the below.</div><div><br /></div><div>5) Install aws-sdk package: In your node js application, install aws-sdk package by using command like npm install aws-sdk</div><div><br /></div><div>6) Create routes for handling files: Below is the code for different routes like upload a file in S3, get list of all the files from the S3 bucket, delete a file, download a file.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">dotenv</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'dotenv'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">path</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'path'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">multer</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'multer'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">AWS</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'aws-sdk'</span>);</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">s3</span> = <span style="color: #569cd6;">new</span> <span style="color: #4fc1ff;">AWS</span>.<span style="color: #4ec9b0;">S3</span>();</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>();</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">upload</span> = <span style="color: #dcdcaa;">multer</span>();</div><div><span style="color: #9cdcfe;">dotenv</span>.<span style="color: #dcdcaa;">config</span>({</div><div> <span style="color: #9cdcfe;">path</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'./.env'</span>)</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #b5cea8;">8000</span>);</div><br /><div><span style="color: #6a9955;">//Creating routes</span></div><div><span style="color: #6a9955;">//uploading file to s3</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/upload'</span>, <span style="color: #4fc1ff;">upload</span>.<span style="color: #dcdcaa;">single</span>(<span style="color: #ce9178;">'file'</span>), <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">s3</span>.<span style="color: #dcdcaa;">putObject</span>({</div><div> <span style="color: #9cdcfe;">Body</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">file</span>.<span style="color: #9cdcfe;">buffer</span>,</div><div> <span style="color: #9cdcfe;">Bucket</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">BUCKET_NAME</span>,</div><div> <span style="color: #9cdcfe;">Key</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">file</span>.<span style="color: #9cdcfe;">originalname</span></div><div> }).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div>});</div><br /><div><span style="color: #6a9955;">//Listing uploaded files</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/fileList'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">s3</span>.<span style="color: #dcdcaa;">listObjectsV2</span>({</div><div> <span style="color: #9cdcfe;">Bucket</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">BUCKET_NAME</span></div><div> }).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>});</div><br /><div><span style="color: #6a9955;">//Deleting a file</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">delete</span>(<span style="color: #ce9178;">'/deleteFile/:fileName'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">fileName</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">params</span>.<span style="color: #9cdcfe;">fileName</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">s3</span>.<span style="color: #dcdcaa;">deleteObject</span>({</div><div> <span style="color: #9cdcfe;">Bucket</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">BUCKET_NAME</span>,</div><div> <span style="color: #9cdcfe;">Key</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">fileName</span></div><div> }).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>})</div><br /><div><span style="color: #6a9955;">//Download a file</span></div><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/downloadFile/:fileName'</span>, <span style="color: #569cd6;">async</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">fileName</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">params</span>.<span style="color: #9cdcfe;">fileName</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">s3</span>.<span style="color: #dcdcaa;">getObject</span>({</div><div> <span style="color: #9cdcfe;">Bucket</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">BUCKET_NAME</span>,</div><div> <span style="color: #9cdcfe;">Key</span><span style="color: #9cdcfe;">:</span> <span style="color: #4fc1ff;">fileName</span></div><div> }).<span style="color: #dcdcaa;">promise</span>();</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">Body</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>})</div><br /><br /><br /></div></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-37458516106608407612022-02-01T23:27:00.005-08:002022-02-01T23:29:30.283-08:00Cluster management in node js application by PM2 Lib<p> In my previous blog, we discussed about how to manually manage cluster in node js application by writing the required code. In production mode, we may have to write more code to manage it.</p><p>Link: <a href="https://jksnu.blogspot.com/2022/02/cluster-in-node-js-application.html" target="_blank">Cluster in node js application</a></p><p>We can avoid writing the manual code to manage the cluster in node js application. It can be done by using PM2 npm lib. Now, suppose we have below code where express js is creating a server and listening a port and we have to make it cluster supported by using PM2 lib.</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">8000</span>;</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>(); </div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">i</span>=<span style="color: #b5cea8;">0</span>; <span style="color: #9cdcfe;">i</span><<span style="color: #b5cea8;">999999999999999</span>; <span style="color: #9cdcfe;">i</span>++) {</div><br /><div> }</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'hello world'</span>);</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/test'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'Quickly say Hello World'</span>);</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`app is listening at port </span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">port</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div>});</div><br /><br /><br /><br /></div><p>Here, not a single line is written to manage cluster and if we run it and trigger "/" route first then it will keep running for few minutes as it is processing a large loop and in the mean time if we trigger "/test" route, it will have to wait till the completion of first route.</p><p>Now, if we enable cluster with multiple processes, then 2nd route will not have to wait for the completion of the first route. It can be done by PM2 lib as given below.</p><p>First, install PM2 lib at global level by using command like:</p><p>npm install pm2 -g</p><p>Now, if we are working in Windows environment, we can open cmd and go to the path where our app.js file is located where server creation code is written and run the command like:</p><p>pm2 start app1.js -i -1</p><p>This command will spawn number of processes = Number of CPU - 1</p><p>We can see the list of these initiated processes with below command:</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhsG1f03mhC-wuB_yyWkP7igsjhKYWDA9VrVyDXnazX3Af9_i4ryWDCvijlxm4nj7hqlPHhUzHngYAci4_z1wPnIJ3dVBOnDeVx3eCFmjp-AzFHHG5Sdf9BabSWajNgdy9dYiMokFCVorkR-oLVlGE6iml0fXYswzFprypryZTGLhwCJ0Vxg14Gr6pw" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="105" data-original-width="1075" height="53" src="https://blogger.googleusercontent.com/img/a/AVvXsEhsG1f03mhC-wuB_yyWkP7igsjhKYWDA9VrVyDXnazX3Af9_i4ryWDCvijlxm4nj7hqlPHhUzHngYAci4_z1wPnIJ3dVBOnDeVx3eCFmjp-AzFHHG5Sdf9BabSWajNgdy9dYiMokFCVorkR-oLVlGE6iml0fXYswzFprypryZTGLhwCJ0Vxg14Gr6pw=w549-h53" width="549" /></a></div><p>We can monitor all the processes status by command like:</p><p>pm2 monit</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEjBf8ro6ABNKhC3uzobUF7621trYExpf5yvMOHwMCbZvoYwLf4immPDRravaKVpCX6rfmwx1F48hsdlXH6ei_zkbji1mSGSFe580ZI22BD1SVlK1yKGn3UHr40o-73v1jYK-2JAJ_F0K5s5QGtCJo2CfVXhHtLPkPgNg5OcgFFFAmsJlcoKBeQyXX1N" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="682" data-original-width="1004" height="332" src="https://blogger.googleusercontent.com/img/a/AVvXsEjBf8ro6ABNKhC3uzobUF7621trYExpf5yvMOHwMCbZvoYwLf4immPDRravaKVpCX6rfmwx1F48hsdlXH6ei_zkbji1mSGSFe580ZI22BD1SVlK1yKGn3UHr40o-73v1jYK-2JAJ_F0K5s5QGtCJo2CfVXhHtLPkPgNg5OcgFFFAmsJlcoKBeQyXX1N=w490-h332" width="490" /></a></div><br />If needed we can stop all or any one process by using command like:<p></p><p>pm2 stop all</p><p>pm2 stop id</p><p>Till, here, we have seen that we are able to initiate number of processes in node application by using pm2 commands where we provides the required number of processes each time we are running the command. We can create a config js file where we can add the required config related with cluster and now pm2 command we always read from this config file and accordingly it will start the processes. To create the config file, we can use below command like:</p><p>pm2 ecosystem</p><p>It will create a file named as ecosystem.config.js with some required config. We can add the required changes in this config file as per our requirement. The config file will look like as follows:</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = {</div><div> <span style="color: #9cdcfe;">apps</span><span style="color: #9cdcfe;"> :</span> [{</div><div> <span style="color: #9cdcfe;">script</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'app1.js'</span>,</div><div> <span style="color: #9cdcfe;">watch</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'.'</span>,</div><div> <span style="color: #9cdcfe;">instances</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">0</span>,</div><div> <span style="color: #9cdcfe;">exec_mode</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"cluster"</span></div><div> }],</div><br /><div> <span style="color: #9cdcfe;">deploy</span><span style="color: #9cdcfe;"> :</span> {</div><div> <span style="color: #9cdcfe;">production</span><span style="color: #9cdcfe;"> :</span> {</div><div> <span style="color: #9cdcfe;">user</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'SSH_USERNAME'</span>,</div><div> <span style="color: #9cdcfe;">host</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'SSH_HOSTMACHINE'</span>,</div><div> <span style="color: #9cdcfe;">ref</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'origin/master'</span>,</div><div> <span style="color: #9cdcfe;">repo</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'GIT_REPOSITORY'</span>,</div><div> <span style="color: #9cdcfe;">path</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'DESTINATION_PATH'</span>,</div><div> <span style="color: #ce9178;">'pre-deploy-local'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">''</span>,</div><div> <span style="color: #ce9178;">'post-deploy'</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'npm install && pm2 reload ecosystem.config.js --env production'</span>,</div><div> <span style="color: #ce9178;">'pre-setup'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">''</span></div><div> }</div><div> }</div><div>};</div><br /></div><p>Now, to start cluster by using PM2 command by utilizing the above ecosystem.config.js file, we need to run below command like:</p><p>pm2 start ecosystem.config.js</p><p>This is all about PM2 lib. We can find more details from https://www.npmjs.com/package/pm2</p><p><br /></p><p><br /></p><br /><br /><p></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-56891065071734208042022-02-01T03:53:00.003-08:002022-02-01T03:58:24.999-08:00Cluster in node js application<p> Whenever we create a simple node js and express application, it runs with main process without any cluster functionality. Generally, application works well except in some scenarios where a particular incoming request is taking more time. In such cases, the main process remains occupied with this request and in the mean time if any request to any other route comes, it will wait till the completion of previous request. It can be understood with below code.</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">8000</span>;</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>(); </div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">i</span>=<span style="color: #b5cea8;">0</span>; <span style="color: #9cdcfe;">i</span><<span style="color: #b5cea8;">9999999999999999999</span>; <span style="color: #9cdcfe;">i</span>++) {</div><br /><div> }</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'hello world'</span>);</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/test'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'Quickly say Hello World'</span>);</div><div>});</div><br /><div><span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`app is listening at port </span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">port</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div>});</div><br /><br /><br /><br /></div><p>Here, we have created a simple node express application with two GET routes:</p><p>1) /test: It is just returning a message.</p><p>2) "/": It is running a very large loop and then returning the message after few minutes</p><p>Now, if we first run the route "/" in one browser, it will start running and in the mean time if we trigger the "/test" route in another browser, it will be in waiting mode till the first one is completed. If we go through the</p><p> <span style="background-color: #1e1e1e; color: #9cdcfe; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">console</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">.</span><span style="background-color: #1e1e1e; color: #dcdcaa; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">log</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">(</span><span style="background-color: #1e1e1e; color: #ce9178; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">`Incoming req accepted by Process </span><span style="background-color: #1e1e1e; color: #569cd6; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">${</span><span style="background-color: #1e1e1e; color: #9cdcfe; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">process</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">.</span><span style="background-color: #1e1e1e; color: #4fc1ff; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">pid</span><span style="background-color: #1e1e1e; color: #569cd6; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">}</span><span style="background-color: #1e1e1e; color: #ce9178; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">`</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">);</span></p><p>for both the routes in our terminal, we will find that both the requests were processed by same processId and that is why second route was waiting till first was running.</p><p>The above problem can be solved by using Node Js Cluster functionality. Here, we will create as many child processes as number of Core in our Operating system are present. Each of these child processes will listen to same port. This can be seen by below code:</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">http</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'http'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">cluster</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'cluster'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">os</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'os'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #9cdcfe;">process</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'process'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">port</span> = <span style="color: #b5cea8;">8000</span>;</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">numCPUs</span> = <span style="color: #4ec9b0;">os</span>.<span style="color: #dcdcaa;">cpus</span>().<span style="color: #9cdcfe;">length</span>;</div><div><span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`No of cpu = </span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">numCPUs</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><br /><div><span style="color: #6a9955;">//Creating child processes from main process based on number of cpus</span></div><div><span style="color: #c586c0;">if</span> (<span style="color: #4ec9b0;">cluster</span>.<span style="color: #9cdcfe;">isMaster</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Primary </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> is running`</span>);</div><br /><div> <span style="color: #6a9955;">// Fork workers.</span></div><div> <span style="color: #c586c0;">for</span> (<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">i</span> = <span style="color: #b5cea8;">0</span>; <span style="color: #9cdcfe;">i</span> < <span style="color: #4fc1ff;">numCPUs</span>; <span style="color: #9cdcfe;">i</span>++) {</div><div> <span style="color: #4ec9b0;">cluster</span>.<span style="color: #dcdcaa;">fork</span>();</div><div> }</div><br /><div> <span style="color: #4ec9b0;">cluster</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'exit'</span>, (<span style="color: #9cdcfe;">worker</span>, <span style="color: #9cdcfe;">code</span>, <span style="color: #9cdcfe;">signal</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`worker </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">worker</span>.<span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> died`</span>);</div><div> <span style="color: #4ec9b0;">cluster</span>.<span style="color: #dcdcaa;">fork</span>();</div><div> });</div><div>} <span style="color: #c586c0;">else</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">app</span> = <span style="color: #dcdcaa;">express</span>(); </div><br /><div> <span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">i</span>=<span style="color: #b5cea8;">0</span>; <span style="color: #9cdcfe;">i</span><<span style="color: #b5cea8;">9999999999999999999</span>; <span style="color: #9cdcfe;">i</span>++) {</div><div> </div><div> }</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'hello world'</span>);</div><div> });</div><br /><div> <span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/test'</span>, (<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`Incoming req accepted by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> <span style="color: #9cdcfe;">res</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">'Quickly say Hello World'</span>);</div><div> });</div><br /><div> <span style="color: #4fc1ff;">app</span>.<span style="color: #dcdcaa;">listen</span>(<span style="color: #4fc1ff;">port</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">`app is listening at port </span><span style="color: #569cd6;">${</span><span style="color: #4fc1ff;">port</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> by Process </span><span style="color: #569cd6;">${</span><span style="color: #9cdcfe;">process</span>.<span style="color: #4fc1ff;">pid</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;">`</span>);</div><div> });</div><div>}</div><br /><br /><br /><br /></div><p>Here, the code <span style="background-color: #1e1e1e; color: #4ec9b0; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">os</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">.</span><span style="background-color: #1e1e1e; color: #dcdcaa; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">cpus</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">().</span><span style="background-color: #1e1e1e; color: #9cdcfe; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">length</span> defines the number of cores in our OS and accordingly we are creating the child processes by using fork() function. Here, each child process is listening the port 8000.</p><p>Now, if we first run the route "/" it will start running and before its completion if we run the second route "/test" in another browser, it will be processed by another child process and it will not wait for the completion of first route. It will be completed immediately.</p><p>If we perform load testing, we will get very good result comparing to the first one. But, one thing important to note here is that, spawning lot of child processes are not helpful because each child process requires some resource and if number of child processes increases it may inversely impact the performance. </p><p>This is very simple way of implementation of cluster in node application but in Production environment, it may not be so simple. To automatically manage clustering in node application without manually writing this code, we can use a library named as PM2. In my next blog, I will discuss about it. </p><p><br /></p><p><br /></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-48988427883433160422021-09-09T21:24:00.008-07:002021-09-09T21:26:56.652-07:00How to create a Post request with Multipart header to upload a file through Postman<p> <b>Scenario:</b></p><p>Create a Post Request to upload two files along with some input data through Postman.</p><p><b>Note</b>: Handling of this Post Request for file uploading will be discussed in next Blog.</p><p><b>Steps:</b></p><p></p><ul style="text-align: left;"><li>Open Postman and create a Post request </li></ul><div><span><span><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzTaRDuSIjzupJ46u_AnGZxzgYcKvu_FKoSyM0i5OipdcCW_ugR_YMAydhTdw_SAaP26b5RyuUzHgIqwCgyNbBA6pN628SdYIhpo3zj2R41z8fqm7cxtgpHy2blQcBRSG47WLI0hVtXa4/s982/1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="350" data-original-width="982" height="143" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzTaRDuSIjzupJ46u_AnGZxzgYcKvu_FKoSyM0i5OipdcCW_ugR_YMAydhTdw_SAaP26b5RyuUzHgIqwCgyNbBA6pN628SdYIhpo3zj2R41z8fqm7cxtgpHy2blQcBRSG47WLI0hVtXa4/w400-h143/1.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: left;"><ul style="text-align: left;"><li>Click on the Body tab and then select form-data radio button</li></ul><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJhz6Cy-Vp7i40ZKkduAQOJ7HXsxGTK89LpjWYk5FqmghkGQSJr3DWn_EuRgHJH2WFxvMiT3C3dAqIHkIjgN_-_NLypg1pRBGqtZOWw9TwgEnvnLnnQq5TjYObIsV3k0z64htYLoYWrX0/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="342" data-original-width="975" height="140" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJhz6Cy-Vp7i40ZKkduAQOJ7HXsxGTK89LpjWYk5FqmghkGQSJr3DWn_EuRgHJH2WFxvMiT3C3dAqIHkIjgN_-_NLypg1pRBGqtZOWw9TwgEnvnLnnQq5TjYObIsV3k0z64htYLoYWrX0/w400-h140/image.png" width="400" /></a></div><div><br /></div><ul style="text-align: left;"><li>Enter the field name like "myfile1" in textbox in column with Header KEY and then select "File" from the drop down</li></ul><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghA-fEFIIFWF_nNqfpktmuntcOLrPlZBJ0YuUn3CQBsZEAJERV2dRlK7RWnCm7Dzmr57aeH3Ji_oiYep1eDG4Of_hp0j1JV-X7Iu2k-8BRqBm95zXgRUgqW7fi7L-RrBL6hBPQJIIbIIE/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="458" data-original-width="972" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghA-fEFIIFWF_nNqfpktmuntcOLrPlZBJ0YuUn3CQBsZEAJERV2dRlK7RWnCm7Dzmr57aeH3Ji_oiYep1eDG4Of_hp0j1JV-X7Iu2k-8BRqBm95zXgRUgqW7fi7L-RrBL6hBPQJIIbIIE/w400-h189/image.png" width="400" /></a></div><br /><ul style="text-align: left;"><li>Once you select File from the dropdown, you will get a button "Select Files" under the column VALUE. You can click on this button and can select the file that you want to upload.</li></ul><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGdluht_0aepQBX1Xwmwak7BWKvlPw2lIpAac24AZD8UNW11CoNQMPCgrfCGBy1LyrT3v4Cm-xjBWwHGPv9zcbBWg_R7AlC_kwNtz0XBLS6dy6A4SgpZ8eF24tyel_s8b7Kfkd1ZF2-yQ/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="368" data-original-width="986" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGdluht_0aepQBX1Xwmwak7BWKvlPw2lIpAac24AZD8UNW11CoNQMPCgrfCGBy1LyrT3v4Cm-xjBWwHGPv9zcbBWg_R7AlC_kwNtz0XBLS6dy6A4SgpZ8eF24tyel_s8b7Kfkd1ZF2-yQ/w400-h149/image.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTUNsjCe8O_otBy-ajv48bolwBvnqqBHAvuWIDzPHnRgwKBxOwc4FkJttVj5fuMEOjA1kCX4mASSwm-ADeHACls0LQYfmh9IohGMSUD-yunioxJv90AdrL9ar6RdiAZxMYVxWYSRg5ywg/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="381" data-original-width="980" height="155" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTUNsjCe8O_otBy-ajv48bolwBvnqqBHAvuWIDzPHnRgwKBxOwc4FkJttVj5fuMEOjA1kCX4mASSwm-ADeHACls0LQYfmh9IohGMSUD-yunioxJv90AdrL9ar6RdiAZxMYVxWYSRg5ywg/w400-h155/image.png" width="400" /></a></div><br /><ul style="text-align: left;"><li>Now, suppose you want to send name and emailId and input data with this Post request. So, you should add the field names like "name" and "emailId" in KEY column and corresponding values in VALUE column. Here, by default, TEXT remains selected.</li></ul><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj22UGA3IAJBTEdjT1RksKDvUPn2U3fTlNG8shKG8aGNLIBSQbZVis_5xiEuetwUU9-ZJJVYbCU30qZpK7JkAwLkSUXVq2D4OaXvwjyBV29Vsn7EW9niA0zIUIvUQivyVcKOkyO6eAZcBc/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="443" data-original-width="981" height="181" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj22UGA3IAJBTEdjT1RksKDvUPn2U3fTlNG8shKG8aGNLIBSQbZVis_5xiEuetwUU9-ZJJVYbCU30qZpK7JkAwLkSUXVq2D4OaXvwjyBV29Vsn7EW9niA0zIUIvUQivyVcKOkyO6eAZcBc/w400-h181/image.png" width="400" /></a></div><br /><br /></div></div><br /></div></div><br /></div></div><br /></span></span><br /></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-39006676615047060532021-08-29T00:31:00.002-07:002021-08-29T00:33:51.330-07:00Download file from Azure file and save it in temp folder in server by using Node js<p> <b>Scenario</b>: </p><p>Download excel file from Azure file system and process it in server through Node Js to generate some meaningful reports.</p><p><b>Pre-requisite</b>:</p><p>Here, I am going to assume that as a developer, we are already having below information or tasks ready to write required code to fix the above scenario:</p><p></p><ol style="text-align: left;"><li>A running node js application where we can add required code to read files from Azure file system and download it to our server where node js is running.</li><li>Install package like: @azure/storage-file-share</li><li>Required credentials to connect with your Azure storage like AccountName and AccountKey</li></ol><div><b>Note</b>: Here, I am presenting only the way of getting excel files from Azure file system and saving it in your server. To see, how your NodeJs code can read these files from your node js server and process every sheets of every excel file, you can go through my another blog:</div><div><br /></div><div><a href="https://jksnu.blogspot.com/2021/07/read-all-sheets-of-excel-file-in-nodejs.html" target="_blank"><b>Read all sheets of an excel file in Node Js using XLSX package</b></a></div><div><br /></div><div><b>Implementation:</b></div><div><b><br /></b></div><div>1) This implementation can be done in following steps:Create azure connection string: Keep your Azure AccountName and AccountKey somewhere safe like in your environment variable or DB. Create connection string by using these two data in following way: </div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><span style="color: #9cdcfe;">"azureStorageConnectionString"</span>: <span style="color: #ce9178;">"DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey;EndpointSuffix=core.windows.net"</span>,</div></div><div><br /></div><div>2) Create a temp directory in your server where your node js code is running so that your node js code can further read and process the downloaded files from this temp directory.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">tempDir</span> = <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'../../temp_dir_name'</span>);</div></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div style="line-height: 19px;"><div><span style="color: #6a9955;">//creating temp folder path </span></div><div><span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">mkdirSync</span>(<span style="color: #4fc1ff;">tempDir</span>, { <span style="color: #9cdcfe;">recursive</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span> })</div></div></div><div><br /></div><div>3) Write code for downloading file from Azure file system.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div style="line-height: 19px;"><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">azurefileClient</span> = <span style="color: #4fc1ff;">serviceClient</span></div><div> .<span style="color: #dcdcaa;">getShareClient</span>(<span style="color: #4fc1ff;">shareName</span>)</div><div> .<span style="color: #9cdcfe;">rootDirectoryClient</span>.<span style="color: #dcdcaa;">getFileClient</span>(<span style="color: #9cdcfe;">filePathInAzureFileSystem</span>);</div><div> <span style="color: #6a9955;">// Get file content from azure file system</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">azureFileResponse</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">azurefileClient</span>.<span style="color: #dcdcaa;">download</span>();</div><div> <span style="color: #4fc1ff;">azureFileResponse</span>.<span style="color: #9cdcfe;">readableStreamBody</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'end'</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"File downloaded successfully but yet to be written on nodejs server"</span>);</div><div> });</div></div></div></div><div><br /></div><div>Here, we are creating a read stream i.e. <span style="background-color: #1e1e1e; color: #4fc1ff; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">azureFileResponse</span> for reading the file (represented by filePathInAzureFileSystem having the path as well as file name like ext-files/abc/xyz/readMe.xlsx) in binary format from azure file sytem. We are attaching one event handler on this read stream to notify once reading is completed.</div><div><br /></div><div>4) Now, we are going to create one write stream that will take input from the above read stream and write the data in the temp folder in the same file name in nodejs server.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">//creating write stream</span></div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">writer</span> = <span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">createWriteStream</span>(<span style="color: #4fc1ff;">tempDir</span> + <span style="color: #ce9178;">"/"</span> + <span style="color: #ce9178;">"fileName.xlsx"</span>);</div><div> <span style="color: #9cdcfe;">writer</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'error'</span>, (<span style="color: #9cdcfe;">err</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">err</span>, <span style="color: #ce9178;">"Error in writing the downloaded file in nodejs server"</span>);</div><div> });</div></div></div><div><br /></div><div>Here, what ever data is being read from azure read stream, that data will be written to the file tempDir/fileName.xlsx file in node js server. Here, we have attached an error event handler with temp file write stream to handle any error if it happens during writing data to temp file.</div><div><br /></div><div>5) Connect the azure file read stream with temp file write stream through node js pipe functionality so that temp file write stream will receive chucks of data from azure read stream and will keep writing to temp file in nodejs env.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">//connecting readStream with write Stream to write downloaded file into temp folder in node js server</span></div><div> <span style="color: #c586c0;">return</span> <span style="color: #c586c0;">await</span> <span style="color: #dcdcaa;">pipeline</span>(<span style="color: #4fc1ff;">azureFileResponse</span>.<span style="color: #9cdcfe;">readableStreamBody</span>, <span style="color: #9cdcfe;">writer</span>);</div></div></div><div><br /></div><div>Following is the complete code:</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> { <span style="color: #4ec9b0;">ShareServiceClient</span> } = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"@azure/storage-file-share"</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">fs</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'fs'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">path</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'path'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">stream</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'stream'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">pipeline</span> = <span style="color: #9cdcfe;">util</span>.<span style="color: #dcdcaa;">promisify</span>(<span style="color: #4ec9b0;">stream</span>.<span style="color: #dcdcaa;">pipeline</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">shareName</span> = <span style="color: #9cdcfe;">config</span>.<span style="color: #9cdcfe;">shareName</span>; </div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">tempDir</span> = <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'../../temp_dir_name'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">azureStorageConnectionStr</span> = <span style="color: #ce9178;">"DefaultEndpointsProtocol=https;AccountName=YourAccountName;AccountKey=YourAccountKey;EndpointSuffix=core.windows.net"</span>;</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">serviceClient</span> = <span style="color: #4ec9b0;">ShareServiceClient</span>.<span style="color: #dcdcaa;">fromConnectionString</span>(<span style="color: #4fc1ff;">azureStorageConnectionStr</span>);</div><br /><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">downloadFileFromAzureFileSystem</span> = <span style="color: #569cd6;">async</span> (<span style="color: #9cdcfe;">filePathInAzureFileSystem</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> { </div><div> <span style="color: #6a9955;">//creating temp folder path </span></div><div> <span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">mkdirSync</span>(<span style="color: #4fc1ff;">tempDir</span>, { <span style="color: #9cdcfe;">recursive</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span> })</div><br /><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">azurefileClient</span> = <span style="color: #4fc1ff;">serviceClient</span></div><div> .<span style="color: #dcdcaa;">getShareClient</span>(<span style="color: #4fc1ff;">shareName</span>)</div><div> .<span style="color: #9cdcfe;">rootDirectoryClient</span>.<span style="color: #dcdcaa;">getFileClient</span>(<span style="color: #9cdcfe;">filePathInAzureFileSystem</span>);</div><div> <span style="color: #6a9955;">// Get file content from azure file system</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">azureFileResponse</span> = <span style="color: #c586c0;">await</span> <span style="color: #4fc1ff;">azurefileClient</span>.<span style="color: #dcdcaa;">download</span>();</div><div> <span style="color: #4fc1ff;">azureFileResponse</span>.<span style="color: #9cdcfe;">readableStreamBody</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'end'</span>, () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"File downloaded successfully but yet to be written on nodejs server"</span>);</div><div> });</div><br /><div> <span style="color: #6a9955;">//creating write stream</span></div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">tempFileWriter</span> = <span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">createWriteStream</span>(<span style="color: #4fc1ff;">tempDir</span> + <span style="color: #ce9178;">"/"</span> + <span style="color: #ce9178;">"fileName.xlsx"</span>);</div><div> <span style="color: #9cdcfe;">tempFileWriter</span>.<span style="color: #dcdcaa;">on</span>(<span style="color: #ce9178;">'error'</span>, (<span style="color: #9cdcfe;">err</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">err</span>, <span style="color: #ce9178;">"Error in writing the downloaded file in nodejs server"</span>);</div><div> });</div><br /><div> <span style="color: #6a9955;">//connecting readStream with write Stream to write downloaded file into temp folder in node js server</span></div><div> <span style="color: #c586c0;">return</span> <span style="color: #c586c0;">await</span> <span style="color: #dcdcaa;">pipeline</span>(<span style="color: #4fc1ff;">azureFileResponse</span>.<span style="color: #9cdcfe;">readableStreamBody</span>, <span style="color: #9cdcfe;">tempFileWriter</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div><br /><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = {</div><div> <span style="color: #dcdcaa;">downloadFileFromAzureFileSystem</span></div><div>}</div></div></div><div><br /></div><div><br /></div><p></p>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-70913792421722416612021-07-21T09:06:00.000-07:002021-07-21T09:06:30.907-07:00Read all sheets of an excel file in NodeJs using XLSX package<b>Scenario</b>: <div>We have an excel file in our application in a temp folder and we need to process data of each sheet of the excel file. I am giving here the screenshots of folder path of our excel file "candidate.xlsx" with its content.</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-HUmGmkpjrK9pqZ6hc0rIjDQcOsq_cihoEpHcINo-nHgSrtLZ9B_XXdtfBs3_sCoC1Qe4zulFng5PRasud3zz9KLPNKrqiX2BDs78RnGkhGvH5Od7FX4bu5dpx4PsUGTor2Ao3Z2R2mA/s240/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="240" data-original-width="227" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-HUmGmkpjrK9pqZ6hc0rIjDQcOsq_cihoEpHcINo-nHgSrtLZ9B_XXdtfBs3_sCoC1Qe4zulFng5PRasud3zz9KLPNKrqiX2BDs78RnGkhGvH5Od7FX4bu5dpx4PsUGTor2Ao3Z2R2mA/s0/Untitled.png" /></a></div><br /><div>Here, We need to process data from each sheets of excel file candidate.xlsx present under folder "temp_file". Below are the screenshots of its two sheets named as "candidates" and "address".</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDLoRptlEiSgJq185wT6cQWQ_8Jt2bOWqfsN41vq-mv2KUDVFIAsJFGAQFIrYhx8a76ILEG1H69ySfsBA-bhRLnnOfN2lgiuaajUbXCsYP3pvGeOtK9JGiAPbT2eiEhfOv9vFSV4as4io/s446/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="413" data-original-width="446" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDLoRptlEiSgJq185wT6cQWQ_8Jt2bOWqfsN41vq-mv2KUDVFIAsJFGAQFIrYhx8a76ILEG1H69ySfsBA-bhRLnnOfN2lgiuaajUbXCsYP3pvGeOtK9JGiAPbT2eiEhfOv9vFSV4as4io/s320/2.png" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><b>Note</b>: In this sheet, the main headers and data like Name, Email, Phone, Date of birth are starting from 3rd row considering first row as 0. So, header start index is 2. This is going to be used later in code where you can understand it.<div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFba7NgHYVjhZRl9KEVI-HZl6m09N9WaR92V728ZaxIxxucepvEAdM1h2XLW2Y0r92mckPyXMWfkrfSx0BxGpWp9HfFDah3ANKuRrnA84mEcxh7jdJccl4ECNcpvs3K2p5aF1UMv3ENKE/s407/3d.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="407" data-original-width="333" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFba7NgHYVjhZRl9KEVI-HZl6m09N9WaR92V728ZaxIxxucepvEAdM1h2XLW2Y0r92mckPyXMWfkrfSx0BxGpWp9HfFDah3ANKuRrnA84mEcxh7jdJccl4ECNcpvs3K2p5aF1UMv3ENKE/s320/3d.png" /></a></div><br /><div><b>Note</b>: In this sheet, the main headers and data like Country, State, City are starting from 4rth row considering first row as 0. So, header start index is 3. This is going to be used later in code where you can understand it.<div><br /></div><div><b>Solution</b>: </div><div>There are so many npm packages available to process excel file in Javascript but I found XLSX as more reliable and robust. I will write another blog about how to select the best npm package among several available packages.</div></div><div><br /></div><div>The task of processing data of both sheets of above excel file is being performed in following steps:</div><div><br /></div><div><b>Step 1</b>:</div><div>Installed XLSX package by using command like npm install xlsx</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"node_excel_read"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">""</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">"Jitendra Kumar Singh"</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"xlsx"</span>: <span style="color: #ce9178;">"^0.17.0"</span></div><div> }</div><div>}</div></div></div><div><br /></div><div><b>Step 2</b>:</div><div>Get the list of names of all the excel files present in folder "temp_file". Here, I am not recursively traversing in "temp_file" and I am finding the list of file names present directly inside "temp_file".</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * Creating the list of names of files present directly in temp_file dir</span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@returns</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> */</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">getFiles</span> = () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">files</span> = <span style="color: #4ec9b0;">Array</span>();</div><div> <span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">readdirSync</span>(<span style="color: #4fc1ff;">sourceDirPath</span>).<span style="color: #dcdcaa;">forEach</span>(<span style="color: #9cdcfe;">file</span> <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">if</span> (!<span style="color: #4ec9b0;">fs</span>.<span style="color: #dcdcaa;">lstatSync</span>(<span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">resolve</span>(<span style="color: #4fc1ff;">sourceDirPath</span>, <span style="color: #9cdcfe;">file</span>)).<span style="color: #dcdcaa;">isDirectory</span>()) {</div><div> <span style="color: #9cdcfe;">files</span>.<span style="color: #dcdcaa;">push</span>(<span style="color: #9cdcfe;">file</span>);</div><div> }</div><div> });</div><div> <span style="color: #c586c0;">return</span> <span style="color: #9cdcfe;">files</span>;</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div>}</div></div></div><div><br /></div><div><b>Step 3</b>:</div><div>Here, I am going to process only one excel file present inside "temp_file" folder. I will write another blog for recursively traversing "temp_file" folder to get list of all file names and processing all files. Now, I am going to read the excel file by using "xlsx" package.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * Getting the list of name of files present directly in temp_file folder</span></div><div><span style="color: #6a9955;"> * Iterating this list of name of files and then processing each sheet in each file</span></div><div><span style="color: #6a9955;"> * Each sheet is being processed by corresponding function</span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@returns</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> */</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">readTestFile</span> = <span style="color: #569cd6;">async</span> () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">files</span> = <span style="color: #dcdcaa;">getFiles</span>();</div><div> <span style="color: #c586c0;">if</span>(<span style="color: #4fc1ff;">files</span> && <span style="color: #4fc1ff;">files</span>.<span style="color: #9cdcfe;">length</span> === <span style="color: #b5cea8;">0</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"No file present"</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #569cd6;">true</span>;</div><div> }</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">filePath</span> = <span style="color: #4fc1ff;">sourceDirPath</span> + <span style="color: #ce9178;">"/"</span> + <span style="color: #4fc1ff;">files</span>[<span style="color: #b5cea8;">0</span>];</div><div> <span style="color: #6a9955;">//Reading the excel file candidate.xlsx</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">file</span> = <span style="color: #9cdcfe;">xlsx</span>.<span style="color: #dcdcaa;">readFile</span>(<span style="color: #4fc1ff;">filePath</span>);</div><div> <span style="color: #c586c0;">if</span>(<span style="color: #4fc1ff;">file</span> && <span style="color: #4fc1ff;">file</span>.<span style="color: #9cdcfe;">SheetNames</span> && <span style="color: #4fc1ff;">file</span>.<span style="color: #9cdcfe;">SheetNames</span>.<span style="color: #9cdcfe;">length</span> > <span style="color: #b5cea8;">0</span>) {</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">sheetName</span> <span style="color: #569cd6;">of</span> <span style="color: #4fc1ff;">file</span>.<span style="color: #9cdcfe;">SheetNames</span>) {</div><div> <span style="color: #c586c0;">if</span>(<span style="color: #9cdcfe;">sheetName</span> === <span style="color: #ce9178;">'candidates'</span>) {</div><div> <span style="color: #dcdcaa;">processCandidateSheet</span>(<span style="color: #4fc1ff;">file</span>.<span style="color: #9cdcfe;">Sheets</span>[<span style="color: #9cdcfe;">sheetName</span>]);</div><div> } <span style="color: #c586c0;">else</span> <span style="color: #c586c0;">if</span>(<span style="color: #9cdcfe;">sheetName</span> === <span style="color: #ce9178;">'address'</span>) {</div><div> <span style="color: #dcdcaa;">processAddressSheet</span>(<span style="color: #4fc1ff;">file</span>.<span style="color: #9cdcfe;">Sheets</span>[<span style="color: #9cdcfe;">sheetName</span>]);</div><div> }</div><div> }</div><div> }</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> } </div><div>}</div></div></div><div><br /></div><div>Here, I am reading the "candidate.xlsx" file by </div><div><span style="background-color: #1e1e1e; color: #569cd6; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">const</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> </span><span style="background-color: #1e1e1e; color: #4fc1ff; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">file</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;"> = </span><span style="background-color: #1e1e1e; color: #9cdcfe; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">xlsx</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">.</span><span style="background-color: #1e1e1e; color: #dcdcaa; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">readFile</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">(</span><span style="background-color: #1e1e1e; color: #4fc1ff; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">filePath</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">);</span></div><div><br /></div><div>Here, the variable "file" is a list of sheets present in this excel file. I am iterating this "file" variable and processing both the sheets "candidates" and "address" by functions "processCandidateSheet()" and "processAddressSheet()"</div><div><br /></div><div><b>Step 4</b>:</div><div>Here, I am processing "candidates" sheet.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * Processing the data of Candidate sheet</span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@param</span><span style="color: #6a9955;"> </span><span style="color: #4ec9b0;">{*}</span><span style="color: #6a9955;"> </span><span style="color: #9cdcfe;">candidateSheet</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> */</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">processCandidateSheet</span> = (<span style="color: #9cdcfe;">candidateSheet</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">headerStartIndexInCandidateSheet</span> = <span style="color: #b5cea8;">2</span>;<span style="color: #6a9955;">//based on position of header in the sheet</span></div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">candidateJsonData</span> = <span style="color: #dcdcaa;">convertSheetToJson</span>(<span style="color: #9cdcfe;">candidateSheet</span>, <span style="color: #4fc1ff;">headerStartIndexInCandidateSheet</span>);</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">data</span> <span style="color: #569cd6;">of</span> <span style="color: #9cdcfe;">candidateJsonData</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">data</span>);</div><div> }</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div></div></div><div><br /></div><div>If you go through this code block, here I am calling one function convertSheetToJson(). This function actually converts the data of the sheet in JSON format so that it becomes very easy to play with the resultant JSON data. </div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * Converting sheet data in JSON format</span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@param</span><span style="color: #6a9955;"> </span><span style="color: #4ec9b0;">{}</span><span style="color: #6a9955;"> </span><span style="color: #9cdcfe;">sheetName</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@param</span><span style="color: #6a9955;"> </span><span style="color: #4ec9b0;">{*}</span><span style="color: #6a9955;"> </span><span style="color: #9cdcfe;">headerIndex</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@returns</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> */</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">convertSheetToJson</span> = (<span style="color: #9cdcfe;">sheetName</span>, <span style="color: #9cdcfe;">headerIndex</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">config</span> = {</div><div> <span style="color: #9cdcfe;">header</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"A"</span></div><div> }</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">sheetData</span> = <span style="color: #9cdcfe;">xlsx</span>.<span style="color: #4fc1ff;">utils</span>.<span style="color: #dcdcaa;">sheet_to_json</span>(<span style="color: #9cdcfe;">sheetName</span>, <span style="color: #9cdcfe;">config</span>);</div><div> <span style="color: #c586c0;">if</span> (<span style="color: #9cdcfe;">sheetData</span> && <span style="color: #9cdcfe;">sheetData</span>.<span style="color: #9cdcfe;">length</span> > <span style="color: #b5cea8;">0</span>) {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">header</span> = <span style="color: #9cdcfe;">sheetData</span>[<span style="color: #9cdcfe;">headerIndex</span>];</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">loopIndex</span> = <span style="color: #9cdcfe;">headerIndex</span> + <span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">jsonRows</span> = <span style="color: #4ec9b0;">Array</span>();</div><div> <span style="color: #c586c0;">for</span> (<span style="color: #9cdcfe;">loopIndex</span>; <span style="color: #9cdcfe;">loopIndex</span> < <span style="color: #9cdcfe;">sheetData</span>.<span style="color: #9cdcfe;">length</span>; <span style="color: #9cdcfe;">loopIndex</span>++) {</div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">tempObj</span> = {};</div><div> <span style="color: #c586c0;">for</span> (<span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">property</span> <span style="color: #569cd6;">in</span> <span style="color: #9cdcfe;">sheetData</span>[<span style="color: #9cdcfe;">loopIndex</span>]) {</div><div> <span style="color: #9cdcfe;">tempObj</span>[<span style="color: #4fc1ff;">header</span>[<span style="color: #4fc1ff;">property</span>]] = <span style="color: #9cdcfe;">sheetData</span>[<span style="color: #9cdcfe;">loopIndex</span>][<span style="color: #4fc1ff;">property</span>];</div><div> }</div><div> <span style="color: #9cdcfe;">jsonRows</span>.<span style="color: #dcdcaa;">push</span>(<span style="color: #9cdcfe;">tempObj</span>);</div><div> }</div><div> <span style="color: #c586c0;">return</span> <span style="color: #9cdcfe;">jsonRows</span>;</div><div> }</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div></div></div><div><br /></div><div><b>Step 5</b>:</div><div>Here, I am processing the "address" sheet.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * Processing the data of Address sheet</span></div><div><span style="color: #6a9955;"> * </span><span style="color: #569cd6;">@param</span><span style="color: #6a9955;"> </span><span style="color: #4ec9b0;">{*}</span><span style="color: #6a9955;"> </span><span style="color: #9cdcfe;">addressSheet</span><span style="color: #6a9955;"> </span></div><div><span style="color: #6a9955;"> */</span></div><div> <span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">processAddressSheet</span> = (<span style="color: #9cdcfe;">addressSheet</span>) <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">headerStartIndexInAddressSheet</span> = <span style="color: #b5cea8;">3</span>;<span style="color: #6a9955;">//based on position of header in the sheet</span></div><div> <span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">addressJsonData</span> = <span style="color: #dcdcaa;">convertSheetToJson</span>(<span style="color: #9cdcfe;">addressSheet</span>, <span style="color: #4fc1ff;">headerStartIndexInAddressSheet</span>);</div><div> <span style="color: #c586c0;">for</span>(<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">data</span> <span style="color: #569cd6;">of</span> <span style="color: #9cdcfe;">addressJsonData</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">data</span>);</div><div> }</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #c586c0;">throw</span> <span style="color: #9cdcfe;">error</span>;</div><div> }</div><div>}</div></div></div><div><br /></div><div><b>Step 6</b>:</div><div>Now, I am writing here the function that I will trigger from NodeJs terminal to read file.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #dcdcaa;">initiate</span> = <span style="color: #569cd6;">async</span> () <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #c586c0;">await</span> <span style="color: #dcdcaa;">readTestFile</span>();</div><div>}</div><br /><div><span style="color: #dcdcaa;">setTimeout</span>(() <span style="color: #569cd6;">=></span> {</div><div> <span style="color: #dcdcaa;">initiate</span>();</div><div>}, <span style="color: #b5cea8;">2000</span>);</div></div></div><div><br /></div><div>Step 7:</div><div>All the above code I have written in a file named as "readTestFile.js". Here, I have imported following dependencies.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">fs</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'fs'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">path</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'path'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">xlsx</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'xlsx'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">sourceDirPath</span> = <span style="color: #4fc1ff;">path</span>.<span style="color: #dcdcaa;">join</span>(<span style="color: #9cdcfe;">__dirname</span>, <span style="color: #ce9178;">'../../temp_file'</span>);</div></div></div><div><br /></div><div>To run this file I am using command like "node readTestFile"</div><div><br /></div><div><b>Output</b>:</div><div>Once I run the command like "node readTestFile", it triggers the function "readTestFile()" and starts reading both the sheets of the excel file and printing the data of both the sheets in JSON format.</div><div><br /></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #c8c8c8;">D</span>:\<span style="color: #9cdcfe;">jitendra</span>\<span style="color: #9cdcfe;">project</span>\<span style="color: #9cdcfe;">poc</span>\<span style="color: #9cdcfe;">node_excel_read</span>\<span style="color: #9cdcfe;">src</span>\<span style="color: #9cdcfe;">scripts</span>> <span style="color: #9cdcfe;">node</span> <span style="color: #9cdcfe;">readTestFile</span></div><br /><div><span style="color: #9cdcfe;">Data</span> <span style="color: #9cdcfe;">from</span> <span style="color: #9cdcfe;">candidates</span> <span style="color: #9cdcfe;">sheet</span></div><br /><div>{</div><div> <span style="color: #c8c8c8;">Name</span>: <span style="color: #ce9178;">'AAA AA'</span>, </div><div> <span style="color: #c8c8c8;">Email</span>: <span style="color: #ce9178;">'aaa@aa.com'</span>, </div><div> <span style="color: #c8c8c8;">Phone</span>: <span style="color: #b5cea8;">1111111111</span>, </div><div> <span style="color: #ce9178;">'Date of birth'</span>: <span style="color: #b5cea8;">29504</span></div><div>}</div><div>{</div><div> <span style="color: #c8c8c8;">Name</span>: <span style="color: #ce9178;">'BBB BB'</span>,</div><div> <span style="color: #c8c8c8;">Email</span>: <span style="color: #ce9178;">'bbb@bb.com'</span>,</div><div> <span style="color: #c8c8c8;">Phone</span>: <span style="color: #b5cea8;">2222222222</span>,</div><div> <span style="color: #ce9178;">'Date of birth'</span>: <span style="color: #b5cea8;">44145</span></div><div>}</div><div>{</div><div> <span style="color: #c8c8c8;">Email</span>: <span style="color: #ce9178;">'ccc@cc.com'</span>,</div><div> <span style="color: #c8c8c8;">Phone</span>: <span style="color: #b5cea8;">3333333333</span>,</div><div> <span style="color: #ce9178;">'Date of birth'</span>: <span style="color: #b5cea8;">40157</span></div><div>}</div><br /><div><span style="color: #9cdcfe;">Data</span> <span style="color: #9cdcfe;">from</span> <span style="color: #9cdcfe;">address</span> <span style="color: #9cdcfe;">sheet</span></div><br /><div>{ <span style="color: #c8c8c8;">Country</span>: <span style="color: #ce9178;">'India'</span>, <span style="color: #c8c8c8;">State</span>: <span style="color: #ce9178;">'Karnataka'</span>, <span style="color: #c8c8c8;">City</span>: <span style="color: #ce9178;">'Bangalore'</span> }</div><div>{ <span style="color: #c8c8c8;">Country</span>: <span style="color: #ce9178;">'India'</span>, <span style="color: #c8c8c8;">State</span>: <span style="color: #ce9178;">'Delhi'</span>, <span style="color: #c8c8c8;">City</span>: <span style="color: #ce9178;">'New Delhi'</span> }</div><div>{ <span style="color: #c8c8c8;">Country</span>: <span style="color: #ce9178;">'London'</span>, <span style="color: #c8c8c8;">State</span>: <span style="color: #ce9178;">'AAAA'</span>, <span style="color: #c8c8c8;">City</span>: <span style="color: #ce9178;">'BBBB'</span> }</div><br /></div></div><div><br /></div><div><br /></div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-84471825900360146112021-07-18T02:58:00.001-07:002021-07-18T02:58:37.938-07:00How to manage Zoom meeting programmatically - PART 2<p><b>Part 1 link</b>: <a href="https://jksnu.blogspot.com/2021/07/how-to-manage-zoom-meeting.html" target="_blank">How to manage Zoom meeting programmatically - PART 1</a></p><p>In part 1 of this blog, we understood how to register our application with Zoom to get API Key and Secret Key to be used in programmatic interaction of your application with Zoom services. Now, we will understand how to create, update and delete a zoom meeting.</p><p><b>package.json</b></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"node_zoom_exp"</span>,</div><div> <span style="color: #9cdcfe;">"version"</span>: <span style="color: #ce9178;">"1.0.0"</span>,</div><div> <span style="color: #9cdcfe;">"description"</span>: <span style="color: #ce9178;">"Manage zoom meeting programmatically"</span>,</div><div> <span style="color: #9cdcfe;">"main"</span>: <span style="color: #ce9178;">"index.js"</span>,</div><div> <span style="color: #9cdcfe;">"scripts"</span>: {</div><div> <span style="color: #9cdcfe;">"devStart"</span>: <span style="color: #ce9178;">"nodemon server.js"</span>,</div><div> <span style="color: #9cdcfe;">"test"</span>: <span style="color: #ce9178;">"echo </span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;">Error: no test specified</span><span style="color: #d7ba7d;">\"</span><span style="color: #ce9178;"> && exit 1"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"keywords"</span>: [</div><div> <span style="color: #ce9178;">"node"</span>,</div><div> <span style="color: #ce9178;">"express"</span>,</div><div> <span style="color: #ce9178;">"jsonwebtoken"</span></div><div> ],</div><div> <span style="color: #9cdcfe;">"author"</span>: <span style="color: #ce9178;">"Jitendra Kumar Singh"</span>,</div><div> <span style="color: #9cdcfe;">"license"</span>: <span style="color: #ce9178;">"ISC"</span>,</div><div> <span style="color: #9cdcfe;">"dependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"axios"</span>: <span style="color: #ce9178;">"^0.21.1"</span>,</div><div> <span style="color: #9cdcfe;">"dotenv"</span>: <span style="color: #ce9178;">"^8.2.0"</span>,</div><div> <span style="color: #9cdcfe;">"express"</span>: <span style="color: #ce9178;">"^4.17.1"</span>,</div><div> <span style="color: #9cdcfe;">"jsonwebtoken"</span>: <span style="color: #ce9178;">"^8.5.1"</span>,</div><div> <span style="color: #9cdcfe;">"request"</span>: <span style="color: #ce9178;">"^2.88.2"</span>,</div><div> <span style="color: #9cdcfe;">"request-promise"</span>: <span style="color: #ce9178;">"^4.2.6"</span></div><div> },</div><div> <span style="color: #9cdcfe;">"devDependencies"</span>: {</div><div> <span style="color: #9cdcfe;">"nodemon"</span>: <span style="color: #ce9178;">"^2.0.7"</span></div><div> }</div><div>}</div></div><p><b>Different routes in our node js service</b></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">var</span> <span style="color: #dcdcaa;">express</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'express'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">util</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"../utils/util"</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">ctrl</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"../controllers/mycontroller"</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">auth</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"../middlewares/auth"</span>);</div><br /><div><span style="color: #569cd6;">var</span> <span style="color: #9cdcfe;">router</span> = <span style="color: #dcdcaa;">express</span>.<span style="color: #dcdcaa;">Router</span>();</div><br /><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'/'</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">healthCheck</span>);</div><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/zoomuserInfo'</span>, <span style="color: #9cdcfe;">auth</span>.<span style="color: #dcdcaa;">addToken</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">zoomuserInfo</span>);</div><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/createzoommeeting'</span>, <span style="color: #9cdcfe;">auth</span>.<span style="color: #dcdcaa;">addToken</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">createZoomMeeting</span>);</div><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">'/zoommeeting'</span>, <span style="color: #9cdcfe;">auth</span>.<span style="color: #dcdcaa;">addToken</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">getMeeting</span>);</div><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">patch</span>(<span style="color: #ce9178;">'/updatezoommeeting'</span>, <span style="color: #9cdcfe;">auth</span>.<span style="color: #dcdcaa;">addToken</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">updateMeeting</span>);</div><div><span style="color: #9cdcfe;">router</span>.<span style="color: #dcdcaa;">delete</span>(<span style="color: #ce9178;">'/deletezoommeeting'</span>, <span style="color: #9cdcfe;">auth</span>.<span style="color: #dcdcaa;">addToken</span>, <span style="color: #9cdcfe;">ctrl</span>.<span style="color: #dcdcaa;">deleteMeeting</span>);</div><br /><div><span style="color: #9cdcfe;">module</span>.<span style="color: #9cdcfe;">exports</span> = <span style="color: #9cdcfe;">router</span>;</div><br /></div><p><b>JWT Token for authentication and authorization on zoom side</b></p><p>In every interaction of our application with Zoom services, we have to send JWT token for authentication and authorization to Zoom service. This JWT token is created by using API Key and Secret key that we have received during registration of our application with zoom market place. I am creating this JWT token in a middleware function in auth.js file</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">config</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"../../config"</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">jwt</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"jsonwebtoken"</span>);</div><br /><div><span style="color: #6a9955;">//Use the ApiKey and APISecret from config.js</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">payload</span> = {</div><div> <span style="color: #9cdcfe;">iss</span><span style="color: #9cdcfe;">:</span> <span style="color: #9cdcfe;">config</span>.<span style="color: #9cdcfe;">APIKey</span>,</div><div> <span style="color: #9cdcfe;">exp</span><span style="color: #9cdcfe;">:</span> ((<span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">Date</span>()).<span style="color: #dcdcaa;">getTime</span>() + <span style="color: #b5cea8;">5000</span>)</div><div>};</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">jwt</span>.<span style="color: #dcdcaa;">sign</span>(<span style="color: #4fc1ff;">payload</span>, <span style="color: #9cdcfe;">config</span>.<span style="color: #9cdcfe;">APISecret</span>);</div><br /><div><span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">addToken</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>[<span style="color: #ce9178;">"token"</span>] = <span style="color: #4fc1ff;">token</span>;</div><div> <span style="color: #dcdcaa;">next</span>();</div><div>}</div><br /><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = { <span style="color: #dcdcaa;">addToken</span> }</div></div><p>I have kept API Key and Secret Key in a config file named as config.js as follows:</p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">env</span> = <span style="color: #9cdcfe;">process</span>.<span style="color: #9cdcfe;">env</span>.<span style="color: #4fc1ff;">NODE_ENV</span> || <span style="color: #ce9178;">'production'</span></div><br /><div><span style="color: #6a9955;">/**</span></div><div><span style="color: #6a9955;"> * insert your API Key & Secret for each environment </span></div><div><span style="color: #6a9955;"> * keep this file local and never push it to a public repo for security purposes.</span></div><div><span style="color: #6a9955;"> */</span></div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">config</span> = {</div><div> <span style="color: #9cdcfe;">development</span><span style="color: #9cdcfe;"> :</span>{</div><div> <span style="color: #9cdcfe;">APIKey</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'my_apikey_dev_env'</span>,</div><div> <span style="color: #9cdcfe;">APISecret</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'</span><span style="color: #ce9178;">my_apisecretkey_dev_env</span><span style="color: #ce9178;">'</span></div><div> },</div><div> <span style="color: #9cdcfe;">production</span><span style="color: #9cdcfe;">:</span>{ </div><div> <span style="color: #9cdcfe;">APIKey</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'</span><span style="color: #ce9178;">my_apikey_prod_env</span><span style="color: #ce9178;">'</span>,</div><div> <span style="color: #9cdcfe;">APISecret</span><span style="color: #9cdcfe;"> :</span> <span style="color: #ce9178;">'</span><span style="color: #ce9178;">my_apisecretkey_prod_env</span><span style="color: #ce9178;">'</span></div><div> }</div><div>};</div><br /><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = <span style="color: #4fc1ff;">config</span>[<span style="color: #4fc1ff;">env</span>]</div></div><p><b>Code for different operation with Zoom meeting</b></p><p>Here, I am giving the code for below operation like </p><p></p><ul style="text-align: left;"><li>Get details of the user whose email Id is registered with Zoom market place and going to be used as a host email id for all zoom meetings</li><li>Creating a zoom meeting</li><li>Update a meeting by changing the meeting time, meeting agenda etc.</li><li>Delete a meeting</li><li>Get the details of a meeting</li></ul><div>I have added all these code a file mycontroller.js</div><p></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">const</span> <span style="color: #4ec9b0;">SendResponse</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">"../utils/sendresponse"</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">axios</span> = <span style="color: #dcdcaa;">require</span>(<span style="color: #ce9178;">'axios'</span>);</div><div><span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">sendResponse</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">SendResponse</span>();</div><br /><div><span style="color: #6a9955;">//Health check function</span></div><div><span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">healthCheck</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #ce9178;">"API is running"</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>);</div><div> }</div><div>}</div><div><span style="color: #6a9955;">//Get user detail of host user</span></div><div><span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">zoomuserInfo</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">token</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">email</span> = <span style="color: #ce9178;">'abc@xyz.com'</span>; //host email id</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">"https://api.zoom.us/v2/users/"</span> + <span style="color: #4fc1ff;">email</span>, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer '</span> + <span style="color: #4fc1ff;">token</span>,</div><div> <span style="color: #ce9178;">'User-Agent'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Zoom-api-Jwt-Request'</span>,</div><div> <span style="color: #ce9178;">'content-type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span></div><div> }</div><div> });</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">message</span>);</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> }</div><div>}</div><div><span style="color: #6a9955;">//Create a zoom meeting</span></div><div><span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">createZoomMeeting</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">token</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">email</span> = <span style="color: #ce9178;">'abc@xyz.com'</span>; //host email id;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">post</span>(<span style="color: #ce9178;">"https://api.zoom.us/v2/users/"</span> + <span style="color: #4fc1ff;">email</span> + <span style="color: #ce9178;">"/meetings"</span>, {</div><div> <span style="color: #ce9178;">"topic"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"Discussion about today's Demo"</span>,</div><div> <span style="color: #ce9178;">"type"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">2</span>,</div><div> <span style="color: #ce9178;">"start_time"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"2021-03-18T17:00:00"</span>,</div><div> <span style="color: #ce9178;">"duration"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">20</span>,</div><div> <span style="color: #ce9178;">"timezone"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"India"</span>,</div><div> <span style="color: #ce9178;">"password"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"1234567"</span>,</div><div> <span style="color: #ce9178;">"agenda"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"We will discuss about Today's Demo process"</span>,</div><div> <span style="color: #ce9178;">"settings"</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">"host_video"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"participant_video"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"cn_meeting"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"in_meeting"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"join_before_host"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"mute_upon_entry"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"watermark"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"use_pmi"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"approval_type"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">2</span>,</div><div> <span style="color: #ce9178;">"audio"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"both"</span>,</div><div> <span style="color: #ce9178;">"auto_recording"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"local"</span>,</div><div> <span style="color: #ce9178;">"enforce_login"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"registrants_email_notification"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"waiting_room"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"allow_multiple_devices"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span></div><div> }</div><div> }, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer '</span> + <span style="color: #4fc1ff;">token</span>,</div><div> <span style="color: #ce9178;">'User-Agent'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Zoom-api-Jwt-Request'</span>,</div><div> <span style="color: #ce9178;">'content-type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span></div><div> }</div><div> });</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">message</span>);</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> }</div><div>}</div><div><span style="color: #6a9955;">//Update a zoom meeting</span></div><div><span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">updateMeeting</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">token</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">meetingId</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">meetingId</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">patch</span>(<span style="color: #ce9178;">"https://api.zoom.us/v2/meetings/"</span> + <span style="color: #4fc1ff;">meetingId</span>, {</div><div> <span style="color: #ce9178;">"topic"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"UPDATE: Discussion about today's Demo"</span>,</div><div> <span style="color: #ce9178;">"type"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">2</span>,</div><div> <span style="color: #ce9178;">"start_time"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"2021-03-18T17:00:00"</span>,</div><div> <span style="color: #ce9178;">"duration"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">20</span>,</div><div> <span style="color: #ce9178;">"timezone"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"India"</span>,</div><div> <span style="color: #ce9178;">"password"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"1234567"</span>,</div><div> <span style="color: #ce9178;">"agenda"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"Discussion about how to update zoome meeting programatically"</span>,</div><div> <span style="color: #ce9178;">"settings"</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">"host_video"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"participant_video"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"cn_meeting"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"in_meeting"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"join_before_host"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"mute_upon_entry"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"watermark"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"use_pmi"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"approval_type"</span><span style="color: #9cdcfe;">:</span> <span style="color: #b5cea8;">2</span>,</div><div> <span style="color: #ce9178;">"audio"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"both"</span>,</div><div> <span style="color: #ce9178;">"auto_recording"</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">"local"</span>,</div><div> <span style="color: #ce9178;">"enforce_login"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"registrants_email_notification"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">false</span>,</div><div> <span style="color: #ce9178;">"waiting_room"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span>,</div><div> <span style="color: #ce9178;">"allow_multiple_devices"</span><span style="color: #9cdcfe;">:</span> <span style="color: #569cd6;">true</span></div><div> }</div><div> }, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer '</span> + <span style="color: #4fc1ff;">token</span>,</div><div> <span style="color: #ce9178;">'User-Agent'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Zoom-api-Jwt-Request'</span>,</div><div> <span style="color: #ce9178;">'content-type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span></div><div> }</div><div> });</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">message</span>);</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> }</div><div>}</div><div><span style="color: #6a9955;">//Delete a zoom meeting</span></div><div><span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">deleteMeeting</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">token</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">meetingId</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">meetingId</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">delete</span>(<span style="color: #ce9178;">"https://api.zoom.us/v2/meetings/"</span> + <span style="color: #4fc1ff;">meetingId</span>, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer '</span> + <span style="color: #4fc1ff;">token</span>,</div><div> <span style="color: #ce9178;">'User-Agent'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Zoom-api-Jwt-Request'</span>,</div><div> <span style="color: #ce9178;">'content-type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span></div><div> }</div><div> });</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">message</span>);</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> }</div><div>}</div><div><span style="color: #6a9955;">//Get details of a zoom meeting</span></div><div><span style="color: #569cd6;">async</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">getMeeting</span>(<span style="color: #9cdcfe;">req</span>, <span style="color: #9cdcfe;">res</span>, <span style="color: #9cdcfe;">next</span>) {</div><div> <span style="color: #c586c0;">try</span> {</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">token</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">token</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">meetingId</span> = <span style="color: #9cdcfe;">req</span>.<span style="color: #9cdcfe;">body</span>.<span style="color: #9cdcfe;">meetingId</span>;</div><div> <span style="color: #569cd6;">const</span> <span style="color: #4fc1ff;">result</span> = <span style="color: #c586c0;">await</span> <span style="color: #9cdcfe;">axios</span>.<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">"https://api.zoom.us/v2/meetings/"</span> + <span style="color: #4fc1ff;">meetingId</span>, {</div><div> <span style="color: #9cdcfe;">headers</span><span style="color: #9cdcfe;">:</span> {</div><div> <span style="color: #ce9178;">'Authorization'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Bearer '</span> + <span style="color: #4fc1ff;">token</span>,</div><div> <span style="color: #ce9178;">'User-Agent'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'Zoom-api-Jwt-Request'</span>,</div><div> <span style="color: #ce9178;">'content-type'</span><span style="color: #9cdcfe;">:</span> <span style="color: #ce9178;">'application/json'</span></div><div> }</div><div> });</div><div> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">setSuccess</span>(<span style="color: #b5cea8;">200</span>, <span style="color: #ce9178;">'Success'</span>, <span style="color: #4fc1ff;">result</span>.<span style="color: #9cdcfe;">data</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4fc1ff;">sendResponse</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">res</span>);</div><div> } <span style="color: #c586c0;">catch</span> (<span style="color: #9cdcfe;">error</span>) {</div><div> <span style="color: #9cdcfe;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #9cdcfe;">error</span>.<span style="color: #9cdcfe;">message</span>);</div><div> <span style="color: #dcdcaa;">next</span>();</div><div> }</div><div>}</div><br /><div><span style="color: #4ec9b0;">module</span>.<span style="color: #4ec9b0;">exports</span> = { </div><div> <span style="color: #dcdcaa;">healthCheck</span>, <span style="color: #dcdcaa;">zoomuserInfo</span>, <span style="color: #dcdcaa;">createZoomMeeting</span>, <span style="color: #dcdcaa;">getMeeting</span>, </div><div> <span style="color: #dcdcaa;">updateMeeting</span>, <span style="color: #dcdcaa;">deleteMeeting</span> </div><div>}</div></div><p></p><div>Thus, we can trigger different services of zoom to perform several operation. We can create recurance meeting too programmatically. We can follow below link to get more information about several zoom services available for programmatica use.</div><div><br /></div><div>https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/</div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com4tag:blogger.com,1999:blog-3597209610268531963.post-8437980410536603642021-07-16T08:11:00.040-07:002021-07-18T03:03:21.385-07:00How to manage Zoom meeting programmatically - PART 1<p> As an user, its very simple to manage the Zoom meeting by signing up in Zoom and managing the meeting through Zoom console. But, as a programmer, we need to integrate Zoom video meeting with our application and we need to manage the Zoom meetings programmatically through our application so that our application's user does not required to be registered with Zoom to use Zoom meeting through our application.</p><div><div>If we want to integrate zoom meeting functionality with our application, we need to follow the below steps:</div></div><div><ol style="text-align: left;"><li>Either purchase zoom license or we can use free version also but with some limitation like each meeting with only 40 minute max duration, only one meeting at a time for a host, meeting auto recording in Cloud etc. I am explaining here for free version.</li><li>Register on Zoom(https://zoom.us/signup) with one of your email id. This email id will be used as host email id while creating the zoom meeting programmatically from your application.</li><li>Login to Zoom market place(https://marketplace.zoom.us/) with the credentials generated after signing up in step 2.</li><li>Click on the dropdown "Develop" and select "Build App" as shown in below screenshot </li></ol><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqcLXWYrBEZJHcsJvSKEL1HeH_EoSe9puYZLud_aAeDqCDUJ4T054NkLunp77ygF7B_mw169sqgYVgj73zlwFohc4cl9N4QEEuneVN4RvT8mUM8Y5fJwYwnW6TJer4e7u2a6lbtBISrZM/s1155/Untitled1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="354" data-original-width="1155" height="132" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqcLXWYrBEZJHcsJvSKEL1HeH_EoSe9puYZLud_aAeDqCDUJ4T054NkLunp77ygF7B_mw169sqgYVgj73zlwFohc4cl9N4QEEuneVN4RvT8mUM8Y5fJwYwnW6TJer4e7u2a6lbtBISrZM/w432-h132/Untitled1.png" width="432" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div></div><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;">5. In next page, we should select the required App type. There are several options like JWT, OAuth, Chatbot, SDK, Webhook etc. I am going with JWT. So, click on "Create" button in JWT box. I have already created app with JWT, so I am getting "View here" button instead of "Create". </div></blockquote><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj599FNnytJ6bWJtxhpUeeUd2rtnMNoW8HVJ5r1zMbRvmcyXRVDmKpUCfRuBIA3pYHCfnSkv1rl4Yzg3Beoazcf-kp51An-IvcyLL4cOErE0DdMLssU7Q0I6CheaKsL3ZlNo2s6-S5V0kk/s1162/Untitled2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="539" data-original-width="1162" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj599FNnytJ6bWJtxhpUeeUd2rtnMNoW8HVJ5r1zMbRvmcyXRVDmKpUCfRuBIA3pYHCfnSkv1rl4Yzg3Beoazcf-kp51An-IvcyLL4cOErE0DdMLssU7Q0I6CheaKsL3ZlNo2s6-S5V0kk/w439-h203/Untitled2.png" width="439" /></a></div><br /><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p style="text-align: left;"> 6. Fill the form completely and activate your app</p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p style="text-align: left;">7. Finally, we will reach on our app credential page where we will get API Key and API Secret for our app that we registered with Zoom market place. These two keys will be used to create Authentication token programmatically in our application when our application wants to interact with the exposed services of Zoom to manage video meetings</p></blockquote><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh1ZKB5eont1RkUXCEtSIoy0C_S30Gn2FNc2qpJ587gbLteFryNNo5aYJDcdgEsCfhQwAFBoUSsgbMYMAKceBvlr_mB5g9fjeGA1Js0uKVFnyHz5ssxJroUrxfLNCztur42wd4p4oUZ1o/s1102/Untitled3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="497" data-original-width="1102" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh1ZKB5eont1RkUXCEtSIoy0C_S30Gn2FNc2qpJ587gbLteFryNNo5aYJDcdgEsCfhQwAFBoUSsgbMYMAKceBvlr_mB5g9fjeGA1Js0uKVFnyHz5ssxJroUrxfLNCztur42wd4p4oUZ1o/w462-h208/Untitled3.png" width="462" /></a></div><br /><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p style="text-align: left;"> 8. Save these API key and secret key in your environment configuration variables or in DB in your application from where you can get it whenever required to interact with Zoom services.</p></blockquote><div><div><b>Important Note</b>: This api key and secret key should be kept very secure in your application. These should not be visible to end user.</div></div><div><br /></div><div>Once above all steps are done, we are ready to write code in our application to integrate Zoom meeting feature in our application.</div><div><br /></div><div>I will add required code(in NodeJs language) to create meeting, update meeting, delete meeting in my next blog.</div><div><br /></div><div><b>Part 2 link: <a href="https://jksnu.blogspot.com/2021/07/how-to-manage-zoom-meeting_18.html" target="_blank">How to manage Zoom meeting programmatically - PART 2</a></b></div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com1tag:blogger.com,1999:blog-3597209610268531963.post-69681821213358721542019-11-05T22:22:00.006-08:002019-11-08T21:56:59.723-08:00NodeJs: Module caching<div dir="ltr" style="text-align: left;" trbidi="on">
In NodeJs, whenever we import a module by using require() statement, the corresponding module object is get cached after the first time it is loaded. Now own-wards, if we reload the same module even in different file, we will get a cached copy of the module provided "require.cache" is not modified( has been discussed very clearly in <a href="https://bambielli.com/til/2017-04-30-node-require-cache/">https://bambielli.com/til/2017-04-30-node-require-cache/</a> ).<br />
<br />
This can be understood by following example. Here, I am using 4 classes:<br />
<br />
1) <b>my-module.js</b><br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
class myModule {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>constructor(myName) {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>this.myName = myName;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>}</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>setName(name) {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>this.myName = name;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>}</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
module.exports = new myModule("defaultName");</blockquote>
</blockquote>
Here, my-module is having a field "myName" with default value as "defaultName". This field can be changed either by constructor or by function setName()<br />
<br />
2) <b>my-class1.js</b><br />
<blockquote class="tr_bq">
var myModule = require('./my-module');</blockquote>
<blockquote class="tr_bq">
class myClass1 {<span style="white-space: pre;"> </span></blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>getName() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log("Value of myName in myClass1 object = "+myModule.myName);</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>}</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>changeName(newName) {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>myModule.setName(newName);</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>}</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
module.exports = myClass1;</blockquote>
Here, my-class1.js is importing the module "my-module" and having two functions like getName() and changeName() to print/change value of myName field of "my-module"<br />
<br />
3) <b>my-class2.js</b><br />
<blockquote class="tr_bq">
var myModule = require('./my-module');</blockquote>
<blockquote class="tr_bq">
class myClass2 {<span style="white-space: pre;"> </span></blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>getName() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log("Value of myName in myClass2 object = "+ myModule.myName);</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>}</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
module.exports = myClass2;</blockquote>
Here, it is importing same module i.e. "my-module", but having only one function getName() to print the myName field of "my-module". So, by default the function getName() should print here default value of myName field i.e. "defaultName"<br />
<br />
4) <b>test-class.js</b><br />
<blockquote class="tr_bq">
var myClass1 = require('./my-class1');</blockquote>
<blockquote class="tr_bq">
var myClass1Obj = new myClass1();</blockquote>
<blockquote class="tr_bq">
myClass1Obj.changeName("Jitendra");</blockquote>
<blockquote class="tr_bq">
myClass1Obj.getName();</blockquote>
<blockquote class="tr_bq">
<br /></blockquote>
<blockquote class="tr_bq">
var myClass2 = require('./my-class2');</blockquote>
<blockquote class="tr_bq">
var myClass2Obj = new myClass2();</blockquote>
<blockquote class="tr_bq">
myClass2Obj.getName();</blockquote>
Here, we are first importing my-class1 and creating its object. Then through this object we are changing the default value of myName field of "my-module" and then printing the value of myName field.<br />
<br />
After this, we are importing my-class2 and creating its object and just printing the value of myName field. Here, the value printed is same as the value printed by myClass1 object. That means both my-classs1 and my-class2 are having the same object of "my-module" although both the classes are separately importing that module.<br />
<br />
So here the class my-class1 is first importing the my-module and then object of my-module is getting cached and when my-class2 is going to import the same module, its getting the cached object of my-module instead of fresh new object.<br />
<br />
If we run test-class.js by node command like "node test-class", we will get <b>output like</b>:<br />
<br />
<blockquote class="tr_bq">
Value of myName in myClass1 object = Jitendra</blockquote>
<blockquote class="tr_bq">
Value of myName in myClass2 object = Jitendra</blockquote>
<blockquote class="tr_bq">
</blockquote>
<blockquote class="tr_bq">
</blockquote>
</div>
Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-48175475738074766872019-11-02T21:48:00.001-07:002019-11-02T21:59:25.161-07:00NodeJs: Event emitter: Synchronous - Asynchronous<div dir="ltr" style="text-align: left;" trbidi="on">
We generally use Event emitter class in NodeJs to implement publish subscribe mechanism. Most of us think that this event emitter works totally in asynchronous way. But, its not true. If we take it in scenario wise, we can get two scenarios:<br />
<br />
<b>Scenario 1)</b> Suppose event emitter class is emitting two different events say "event1" and "event2". Here, we usually think that the handlers of both of these events will run asynchronously. But, the fact is that which ever event is triggered first, its handler will run first irrespective of the fact that which event handler is associated first with the event emitter object. e.g.<br />
<br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
const EventEmitter = require('events');</blockquote>
<blockquote class="tr_bq">
class MyEmitter extends EventEmitter {}</blockquote>
<blockquote class="tr_bq">
const myEmitter = new MyEmitter();</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event1', handleEvent1);</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event2', handleEvent2);</blockquote>
<blockquote class="tr_bq">
function handleEvent1() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event1 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<100; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event1 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
function handleEvent2() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event2 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<1000000000; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event2 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
myEmitter.emit('event2');</blockquote>
<blockquote class="tr_bq">
myEmitter.emit('event1');</blockquote>
</blockquote>
The output of the above code is:<br />
<br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
C:\Users\singh\Desktop\delete2>node event-emit-test2</blockquote>
<blockquote class="tr_bq">
an event2 started!</blockquote>
<blockquote class="tr_bq">
an event2 ended!</blockquote>
<blockquote class="tr_bq">
an event1 started!</blockquote>
<blockquote class="tr_bq">
an event1 ended!</blockquote>
</blockquote>
From above output, its clear that "event2" is being triggered first and although its handler is taking more time as compared to handler of "event1", but till the time handler of event2 is running, the handler of event1 is waiting. So, here whichever event is triggered first, it will complete first.<br />
<br />
<b>Scenario 2)</b> Suppose event emitter is emitting same event 2 times and two different handlers are associated with the same event. So, here the rhythm of association matters and which ever handler is associated first, will complete first then the second event handler will start. e.g.<br />
<br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
const EventEmitter = require('events');</blockquote>
<blockquote class="tr_bq">
class MyEmitter extends EventEmitter {}</blockquote>
<blockquote class="tr_bq">
const myEmitter = new MyEmitter();</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event', handleEvent1);</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event', handleEvent2);</blockquote>
<blockquote class="tr_bq">
function handleEvent1() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event1 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<1000000000; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event1 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
function handleEvent2() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event2 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<10; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event2 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
myEmitter.emit('event');</blockquote>
</blockquote>
<blockquote class="tr_bq">
The output is:</blockquote>
<blockquote class="tr_bq">
C:\Users\singh\Desktop\delete2>node event-emit-test2</blockquote>
<blockquote class="tr_bq">
an event1 started!</blockquote>
<blockquote class="tr_bq">
an event1 ended!</blockquote>
<blockquote class="tr_bq">
an event2 started!</blockquote>
<blockquote class="tr_bq">
an event2 ended!</blockquote>
Here, the output is clearly showing that in this scenario, the rhythm of event handler association is being followed and handlers are running in Synchronous way.<br />
<br />
<b>If needed we can force these handlers to run in asynchronous way</b> by using "setImmediate" and "process.nextTick". e.g.<br />
<br />
<blockquote class="tr_bq">
const EventEmitter = require('events');</blockquote>
<blockquote class="tr_bq">
class MyEmitter extends EventEmitter {}</blockquote>
<blockquote class="tr_bq">
const myEmitter = new MyEmitter();</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event', () => setImmediate(handleEvent1));</blockquote>
<blockquote class="tr_bq">
myEmitter.on('event', () => process.nextTick(handleEvent2));</blockquote>
<blockquote class="tr_bq">
function handleEvent1() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event1 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<100; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event1 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
function handleEvent2() {</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span>console.log('an event2 started!');</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> let x=0;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> for(let i=0; i<1000000000; i++){</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> x = x + i;</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> }</blockquote>
<blockquote class="tr_bq">
<span style="white-space: pre;"> </span> console.log('an event2 ended!');</blockquote>
<blockquote class="tr_bq">
}</blockquote>
<blockquote class="tr_bq">
myEmitter.emit('event');</blockquote>
The output is:<br />
<blockquote class="tr_bq">
C:\Users\singh\Desktop\delete2>node event-emit-test</blockquote>
<blockquote class="tr_bq">
an event2 started!</blockquote>
<blockquote class="tr_bq">
an event2 ended!</blockquote>
<blockquote class="tr_bq">
an event1 started!</blockquote>
<blockquote class="tr_bq">
an event1 ended!</blockquote>
It is clear from output that even if handleEvent1 is associated first and handleEvent2 is associated 2nd with event named as "event", but handleEvent2 is being triggered and completed first based on "setImmediate" and "process.nextTick".<br />
<blockquote class="tr_bq">
</blockquote>
<blockquote class="tr_bq">
</blockquote>
<blockquote class="tr_bq">
</blockquote>
</div>
Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-85603174093951675372019-01-10T22:30:00.003-08:002019-01-10T22:36:12.064-08:00How to create repository in GitHub and manage it from Visual Studio version 2017<div dir="ltr" style="text-align: left;" trbidi="on">
GitHub is a repository available online where any developer can register by providing his email id. Once you have registered you can create a new repository there by following below mentioned steps:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<ol style="text-align: left;">
<li>Login into your github account and click on New Repository button<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGzSlH6Oi2znfVlbBPwLhYPzsEecZX_JlSo1giH40dAedpGBP5SZySrvVyzKJgjVboYZsFXgKFxs92Teq-vEkXjLuFQMYO7Yw_WzWEXpwAGNxfSyM3KRO5h5cebW5FYs7ATFdBuzETz2E/s1600/CreateRepository.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="554" data-original-width="847" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGzSlH6Oi2znfVlbBPwLhYPzsEecZX_JlSo1giH40dAedpGBP5SZySrvVyzKJgjVboYZsFXgKFxs92Teq-vEkXjLuFQMYO7Yw_WzWEXpwAGNxfSyM3KRO5h5cebW5FYs7ATFdBuzETz2E/s400/CreateRepository.PNG" width="400" /></a></li>
<li>Fill all the required details in the form that opens after clicking on the New Repository Button. Don't forget to select the checkbox for "Initialize this repository with a ReadMe". It will be helpful in immediate cloning.<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXmby-x8uR5HQhXtcGhmoM7vn6kAw8KVsqB5HaSDTZ_I1UIQNsz3X_S3LlA05W1U3IxwPeSHL61V_djWCcH25Ulj5ICWrgXzvpL9p_ZgG5vtHD4syRYIB4VLostF5OJWnt6rIghyphenhyphenqku7w/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="656" data-original-width="943" height="277" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXmby-x8uR5HQhXtcGhmoM7vn6kAw8KVsqB5HaSDTZ_I1UIQNsz3X_S3LlA05W1U3IxwPeSHL61V_djWCcH25Ulj5ICWrgXzvpL9p_ZgG5vtHD4syRYIB4VLostF5OJWnt6rIghyphenhyphenqku7w/s400/Capture.PNG" width="400" /></a></li>
<li>Now, once you click on Create repository button, your new repository will be ready.<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhltlrjJufCpTadNzEQNF-j5sB6yyeeKR_XkSeGEbLp2bhQJgJPJFRbFfL7R_LpmQASVXnTernZqF6NzpukoAbGHv0s6E6xdM9V_bwGb_RLnMVT4SPQrE2L1Vin1Ijq89cwzaqkynrME3k/s1600/NewRepository.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="673" data-original-width="1140" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhltlrjJufCpTadNzEQNF-j5sB6yyeeKR_XkSeGEbLp2bhQJgJPJFRbFfL7R_LpmQASVXnTernZqF6NzpukoAbGHv0s6E6xdM9V_bwGb_RLnMVT4SPQrE2L1Vin1Ijq89cwzaqkynrME3k/s400/NewRepository.PNG" width="400" /></a></li>
<li>Now, you can take this repository to your local environment by number of ways like by Opening on Desktop, Downloading the zip file for this repository or by creating a fork or clone of this repository. The best and secure way is to use fork or clone. So, click on Clone or download drop down and copy the URL. <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPWvowLsgPeVfMCGwOQI66UXb56Tcs9ygJwdE67gh1-cbRyGo-AqD9uRrDAdB-6OdXinH_BTV2x03GI52L8sNSzOn8uqTNEgcrsIRzY6Od3ZCqlg_rveeeru5nzWZb-K8jX6ZSfFPjAUM/s1600/Clone.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="592" data-original-width="1088" height="217" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPWvowLsgPeVfMCGwOQI66UXb56Tcs9ygJwdE67gh1-cbRyGo-AqD9uRrDAdB-6OdXinH_BTV2x03GI52L8sNSzOn8uqTNEgcrsIRzY6Od3ZCqlg_rveeeru5nzWZb-K8jX6ZSfFPjAUM/s400/Clone.PNG" width="400" /></a></li>
<li>Now, Open your Visual studio code editor and click on Top Menu -> Files -> Open folder and then select the folder in which you want to download your cloned project.<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE84ZLOODLM0BIXhCh3cz34DCG02jQzdrfPMa3ubu3m65ZR_gX0tGHQJHzZYjroQF0Vgk9V4s_scxopHVPT8ZRjWqK0Aln1z7vHADjPCx5_m8RTBl6k4zWXoRA7JHeMdARBicrqsbyhbA/s1600/openVSC.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="632" data-original-width="932" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE84ZLOODLM0BIXhCh3cz34DCG02jQzdrfPMa3ubu3m65ZR_gX0tGHQJHzZYjroQF0Vgk9V4s_scxopHVPT8ZRjWqK0Aln1z7vHADjPCx5_m8RTBl6k4zWXoRA7JHeMdARBicrqsbyhbA/s400/openVSC.PNG" width="400" /></a></li>
<li>Now go to the Menu -> View -> Terminal and execute the command like git clone https://github.com/jksnu/TestRepository.git in the terminal window in visual studio code <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4yTh66s3XkAjbWkpV996f0aacHidQvFnYOiqZOLONYn4oPhXVa6NW4eeydKCQ91zVFoHtu1nXGZ_vtUZ7q9uEbIpL8s0I57j7oApCge-3cXby9LZx4p8t88-X5GaJGCe4BlIA6G9Jnns/s1600/gitClone.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="360" data-original-width="1359" height="105" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4yTh66s3XkAjbWkpV996f0aacHidQvFnYOiqZOLONYn4oPhXVa6NW4eeydKCQ91zVFoHtu1nXGZ_vtUZ7q9uEbIpL8s0I57j7oApCge-3cXby9LZx4p8t88-X5GaJGCe4BlIA6G9Jnns/s400/gitClone.PNG" width="400" /></a></li>
<li>Now, your fork or cloned project is downloaded in your local. You can see your ReadMe.md <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibaNnP-zBKhmAUYTXbeRFKGzjgaoWksnVfeo7YxBAT0iMgkqkr9ElRXudx5BB0ip9ZwV8m9uPaGbsSrEAUGU75qxV88yom3RECInP1UuhpQdiKHbC2wtWSMN8HFxdoPhxl2ZrA8gn9uJU/s1600/downloaded.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="496" data-original-width="1365" height="145" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibaNnP-zBKhmAUYTXbeRFKGzjgaoWksnVfeo7YxBAT0iMgkqkr9ElRXudx5BB0ip9ZwV8m9uPaGbsSrEAUGU75qxV88yom3RECInP1UuhpQdiKHbC2wtWSMN8HFxdoPhxl2ZrA8gn9uJU/s400/downloaded.PNG" width="400" /></a></li>
</ol>
<div>
Thus, here, we understood the following things like:</div>
<div>
<ol style="text-align: left;">
<li>How to register in GitHub</li>
<li>How to create your repository in GitHub</li>
<li>How to Clone your repository in GitHub</li>
<li>How to Checkout your cloned repository from GitHub to your local through Visual Studio code editor</li>
</ol>
<div>
In my next blog, I will discuss about the different steps involved in committing your code to your GitHub repository.</div>
</div>
<div>
<br /></div>
<div>
Feel free to add your suggestion or comments.</div>
<div>
<br /></div>
</div>
Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-7652756615974997272016-02-03T22:08:00.014-08:002021-07-18T03:01:15.592-07:00Collection of my posts<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<!--Place this tag where you want the su badge to render-->
<su:badge layout="1"></su:badge>
<!--Place this snippet wherever appropriate-->
<script type="text/javascript">
(function() {
var li = document.createElement('script'); li.type = 'text/javascript'; li.async = true;
li.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//platform.stumbleupon.com/1/widgets.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(li, s);
})();
</script>
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<div closure_uid_o0y3q1="124">
<span style="font-size: x-large;"><b>JAVA</b></span></div>
<div closure_uid_o0y3q1="124" closure_uid_wkg51b="233">
<br /></div>
<div closure_uid_o0y3q1="124">
<ul style="text-align: left;">
<li closure_uid_wkg51b="250" style="font-family: Verdana, sans-serif;"><span style="font-size: small;"><a closure_uid_o0y3q1="236" href="http://jksnu.blogspot.com/2011/01/serializable-externalizable.html" target="_blank"><span closure_uid_wkg51b="226">Serializable - Externalizable</span> </a></span><span closure_uid_o0y3q1="232" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="232" style="font-size: small;"><a closure_uid_wkg51b="234" href="http://jksnu.blogspot.com/2011/01/java-on-cloud-computing.html" target="_blank">Java on CLOUD Computing</a></span><span closure_uid_o0y3q1="290" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="290" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/01/java-on-cloud-google-app-engine.html" target="_blank">Java on Cloud - Google App Engine</a></span><span closure_uid_o0y3q1="368" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="368" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/02/java-on-cloud-stax-networks.html" target="_blank">Java on Cloud - Stax Networks</a></span><span closure_uid_o0y3q1="406" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="406" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/02/java-job-scheduling.html" target="_blank">Java job scheduling - java.util.Timer</a></span><span closure_uid_o0y3q1="443" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="443" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/03/quartz-framework-implementation-with.html" target="_blank">Quartz Framework: Implementation with Jsp-Servlet</a></span><span closure_uid_o0y3q1="478" style="font-size: small;"></span></li>
<li style="font-family: Verdana, sans-serif;"><span closure_uid_o0y3q1="478" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/05/quartz-framework-implementation-with.html" target="_blank">Quartz Framework: Implementation with Spring Framework</a></span></li>
<li style="font-family: Verdana, sans-serif;"><span style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/07/collection-of-my-posts.html" target="_blank">Interview Questions : Core Java : Volume 1</a></span></li>
<li style="font-family: Verdana, sans-serif;"><span style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/09/interview-questions-core-java-volume-2.html" target="_blank">Interview Questions : Core Java : Volume 2</a></span></li>
<li style="font-family: Verdana, sans-serif;"><span style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/09/interview-questions-jdbc-volume-1.html" target="_blank">Interview Questions : JDBC : Volume 1</a> </span></li>
<li style="font-family: Verdana, sans-serif;"><span style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/09/interview-questions-jdbc-volume-2.html" target="_blank">Interview Questions : JDBC : Volume 2</a></span></li>
<li><span face=""verdana" , sans-serif" style="font-size: small;"><a href="http://jksnu.blogspot.com/2011/09/interview-questions-jdbc-volume-3.html" target="_blank">Interview Questions: JDBC : Volume 3</a></span></li>
<li><span face=""verdana" , sans-serif" style="font-size: small;"><a href="http://jksnu.blogspot.in/2014/08/steps-to-configure-microstrategy-with.html" target="_blank">Steps to configure Microstrategy with Eclipse</a></span></li>
<li><span face=""verdana" , sans-serif" style="font-size: small;"><a href="http://jksnu.blogspot.in/2014/09/steps-to-create-new-plugin-for-bingmap.html" target="_blank">Steps to create new plugin for BingMap by Eclipse in Microstrategy Perspective</a></span></li>
</ul>
</div>
<h1 class="post-title entry-title" closure_uid_7y3egw="148" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana";"></span> </h1>
<h1 class="post-title entry-title" closure_uid_7y3egw="148" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-size: x-large;">Flex </span></h1>
<ul style="text-align: left;">
<li><div class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana"; font-size: 10pt;"><a href="http://jksnu.blogspot.com/2011/06/view-state-difference-between-flex3-and.html" target="_blank"><span closure_uid_wkg51b="271">View State(Flex 3 Implementation): Difference between Flex3 and Flex4 View State Implementation</span></a></span></div>
</li>
<li closure_uid_7y3egw="190"><div class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><a href="http://jksnu.blogspot.com/2011/06/view-state-difference-between-flex3-and_24.html" target="_blank"><span closure_uid_wkg51b="204">View State(Flex 4 Implementation): Difference between Flex3 and Flex4 View </span></a><a closure_uid_wkg51b="204" href="http://jksnu.blogspot.com/2011/06/view-state-difference-between-flex3-and_24.html" span="" target="_blank">State Implementation</a></span></span></div>
</li>
<li closure_uid_7y3egw="190"><div class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><span style="font-family: "verdana"; font-size: 10pt;"><a href="http://jksnu.blogspot.com/2011/06/flex-sdk-41-installation-guide.html" target="_blank">Flex SDK 4.1 Installation Guide</a></span></span></span></div>
</li>
<li closure_uid_7y3egw="190"><div class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 10pt;"><a href="http://jksnu.blogspot.com/2011/07/transition-with-different-effects-like.html" target="_blank"><span closure_uid_7y3egw="216">Transition with different Effects like Resize, Fade in View State in Flex</span></a></span></span></span></span><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span closure_uid_7y3egw="216"> </span></span></span></span></span></div>
</li>
<li closure_uid_7y3egw="190"><div class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 10pt;"><a href="http://jksnu.blogspot.com/2011/09/module-loader-along-with-context.html" target="_blank"><span closure_uid_7y3egw="216">Module Loader along with Context Management in Parsley Framework in Flex 4</span></a></span></span></span></span><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 9pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span style="font-family: "verdana"; font-size: 10pt;"><span closure_uid_7y3egw="216"> </span></span></span></span></span></div>
</li>
</ul>
</div>
</div>
<br />
<h1 class="post-title entry-title" closure_uid_7y3egw="110" closure_uid_l8wxfo="193" closure_uid_o0y3q1="477" closure_uid_wkg51b="195">
<span style="font-size: x-large;">ExtJs and Sencha</span></h1>
<ul style="text-align: left;">
<li><a href="http://jksnu.blogspot.com/2012/08/optimize-performance-of-applications.html" target="_blank">Performance Optimization - Code Minification</a></li>
<li><a href="http://jksnu.blogspot.com/2012/08/optimize-performance-of-applications_28.html" target="_blank">Performance Optimization - Web Storage & Sqlite</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-1.html" target="_blank">Efficient coding style in ExtJs: Volume 1</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-2.html" target="_blank">Efficient coding style in ExtJs: Volume 2</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-3.html" target="_blank">Efficient coding style in ExtJs: Volume 3</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-4.html" target="_blank">Efficient coding style in ExtJs: Volume 4</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-5.html" target="_blank">Efficient coding style in ExtJs: Volume 5</a></li>
<li><a href="http://jksnu.blogspot.in/2013/09/efficient-coding-style-in-extjs-volume-6.html" target="_blank">Efficient coding style in ExtJs: Volume 6</a></li>
<li><a href="http://jksnu.blogspot.in/2012/11/reference-of-this-is-not-required-for_8897.html" target="_blank">Reference of ‘this’ is not required for event handler</a></li>
<li><a href="http://jksnu.blogspot.in/2012/11/use-itemid-instead-of-id.html" target="_blank">Use itemId instead of id</a></li>
<li><a href="http://jksnu.blogspot.in/2013/08/extjs-extdirect-magic.html" target="_blank">Calling Java functions directly in Javascript code by ExtDirect</a></li>
<li><a href="http://jksnu.blogspot.in/2013/08/extdirect-directgngine-example.html" target="_blank">ExtDirect: DirectGNGine Example</a></li>
<li><a href="http://jksnu.blogspot.in/2013/08/network-reliability-by-cometd-hellow_16.html" target="_blank">Network Reliability by CometD: Hello world in CometD</a></li>
<li><a href="http://jksnu.blogspot.in/2013/08/how-to-configure-log4j-in-cometd.html" target="_blank">How to configure log4j in CometD application</a></li>
<li><a href="http://jksnu.blogspot.in/2015/11/javascript-framework-evaluation.html" target="_blank">Javascript framework evaluation: BackBone.js</a></li>
</ul>
<h2 style="text-align: left;">
<b>Others</b></h2>
<div>
<ul style="text-align: left;">
<li><a href="https://jksnu.blogspot.com/2019/01/how-to-commit-in-hit-hub-from-visual.html" target="_blank">How to create repository in GitHub and manage it by Visual Studio code</a></li>
<li><a href="https://jksnu.blogspot.com/2021/07/how-to-manage-zoom-meeting.html" target="_blank">How to manage Zoom meeting programmatically - PART 1</a></li>
<li><a href="https://jksnu.blogspot.com/2021/07/how-to-manage-zoom-meeting_18.html" target="_blank">How to manage Zoom meeting programmatically - PART 2</a></li>
</ul>
</div>
</div>
Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-58230983258002438852015-11-13T22:07:00.003-08:002016-03-17T21:29:38.196-07:00Testing<div dir="ltr" style="text-align: left;" trbidi="on">
<a href="http://elevenhive.com/" target="_blank">Go to Eleven Hive</a></div>
Pixel image
<!--<a href="http://piwik.elevenhive.com/piwik.php?idsite=1&rec=1" imageanchor="1" ><img border="0" src="http://piwik.elevenhive.com/piwik.php?idsite=1&rec=1" /></a>-->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-75286052-1', 'auto');
ga('send', 'pageview');
</script>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-26379417436016535832015-11-13T22:03:00.002-08:002015-11-13T22:14:38.639-08:00Javascript framework evaluation: BackBone.js
<div>
Very important links related to this post:</div>
<div>
<ul style="text-align: left;">
<li><a href="http://jksnu.blogspot.in/" target="_blank"><span style="color: red;">Collection of my posts</span></a></li>
</ul>
</div>
<br /><br />
<div dir="ltr" style="text-align: left;" trbidi="on">
Hi Friends,<br />
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Today, we all know that UI is becoming more and more powerful. With the introduction of Html5, client side code has started becoming more thick. People has started doing lot of work on Javascript and as a result, in last few years several javascript framework has been evolved. Each framework is coming with their some unique and some overlapping features, attributes to make developers life easier. But, it becomes very difficult if have to choose one framework that best suitable for our application. We have also faced similar issue while we were to select a particular framework for our project in our company. At that time, we have evaluated several framework on the basis of certain criterias. I am going to discuss that evaluation of different frameworks one by one in my coming posts:</div>
<br />
Here, I am going to discuss Backbone.<br />
<br />
<div style="text-align: justify;">
Backbone was created by Jeremy Ashkenas who is also known for CoffeeScript and Underscore.js. Its initial release was on 13th Oct, 2010 and current stable version is 1.2.3 released on 19th Aug, 2015. It is a javascript framework based on MVC or MVP pattern. It is very light weight, only 7.3 KB in production, with hard dependency on Underscore.js and JQuery.js and with light dependency on Require.js. So, I am going to start evaluating it on different criteria without boring you more.....</div>
<br />
<table border="1" cellpadding="5" cellspacing="5" style="border-collapse: collapse; width: 546px;">
<colgroup><col style="mso-width-alt: 7058; mso-width-source: userset; width: 145pt;" width="193"></col>
<col style="mso-width-alt: 18761; mso-width-source: userset; width: 385pt;" width="513"></col>
</colgroup><tbody>
<tr height="20" style="height: 15.0pt;">
<td class="xl66" height="20" style="height: 15.0pt; width: 145pt;" width="193"><b>Current
version</b></td>
<td class="xl67" style="border-left: none; width: 385pt;" width="513">1.2.3</td>
</tr>
<tr height="80" style="height: 60.0pt;">
<td class="xl66" height="80" style="border-top: none; height: 60.0pt;"><b>Component
Richness</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
No.
It does not provide any component like textbox, textarea etc. The mote behind
the backbone is mainly to facilitate the communication between these
components with some managed models, views, template along with providing
support for AMD, asynchronous task handling etc.</div>
</td>
</tr>
<tr height="20" style="height: 15.0pt;">
<td class="xl66" height="20" style="border-top: none; height: 15.0pt;"><b>License</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">Mit
License</td>
</tr>
<tr height="60" style="height: 45.0pt;">
<td class="xl66" height="60" style="border-top: none; height: 45.0pt;"><b>Ajax Support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Ajax
is supported in several builtin functions like save, fetch, update, destroy
etc. If we want to use ajax call, then its done by use of $.ajax that is
being provided by jquery in backbone </div>
</td>
</tr>
<tr height="80" style="height: 60.0pt;">
<td class="xl66" height="80" style="border-top: none; height: 60.0pt;"><b>Browser
Cache</b> </td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Browser
caching is possible by using $.ajaxSetUp({cache: true}). Backbone does not
directly provide cache support. It can be implemented by using jquery and
normal html5 tricks. It can also be done by using overriding fetch function
of backbone and providing cache: true here.</div>
</td>
</tr>
<tr height="80" style="height: 60.0pt;">
<td class="xl66" height="80" style="border-top: none; height: 60.0pt;"><b>Mobile Support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Backbone
does not support mobile app development directly. It helps in app development
by giving us way of organizing our code like MVC pattern with model, view and
controller along with Jquery mobile with its feature like routing and mobile
specific components.</div>
</td>
</tr>
<tr height="20" style="height: 15.0pt;">
<td class="xl66" height="20" style="border-top: none; height: 15.0pt;"><b>Browser
Support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">Safari,
Chrome, IE9 onward, Firefox</td>
</tr>
<tr height="160" style="height: 120.0pt;">
<td class="xl66" height="160" style="border-top: none; height: 120.0pt;"><b>SPA Support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">SPA
is supported by Backbone but not directly. SPA is given by backbone by
supporting: <br />
Ajax through
jquery<br />
Templating through
underscore js or HandleBar.js<br />
MVC<br />
Routing<br />
Minification through
uglyfy.js or require.js<br />
AMD through Require.js</td>
</tr>
<tr height="120" style="height: 90.0pt;">
<td class="xl66" height="120" style="border-top: none; height: 90.0pt;"><b>Event
Handling</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Backbone
provides a good event handling machanism by giving functions like on, off,
trigger, once, listenTo, stopListening, listenToOnce. Almost every operation
in backbone can be associated with callback functions for better support of
asynchronous mechanism.</div>
<div style="text-align: justify;">
Although, it does not directly provide a eventBus like mechanism, yet
similar mechanism can be created by using the function mentioned above.</div>
</td>
</tr>
<tr height="80" style="height: 60.0pt;">
<td class="xl66" height="80" style="border-top: none; height: 60.0pt;"><b>Two way
binding</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Here
2 way binding means binding between model and views. This binding is not
directly provided by backbone. It can be done by other tools like knockout.js
or Rivets.js. </div>
<div style="text-align: justify;">
There is a backbone plugin, Model-Binder, which is also a great option for
this.</div>
</td>
</tr>
<tr height="120" style="height: 90.0pt;">
<td class="xl66" height="120" style="border-top: none; height: 90.0pt;"><b>MVC support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
MVC
is supported by backbone. Backbone directly supports model and views by
giving option for creating their object and providing built in function to
manage the life cycle of these objects. Some prefer to have intermediate
"controller" objects that orchestrate the creation and organization
of views into a hierarchy. Others still prefer the evented approach, and
always fire events instead of calling methods directly. All of these styles
work well.</div>
</td>
</tr>
<tr height="100" style="height: 75.0pt;">
<td class="xl66" height="100" style="border-top: none; height: 75.0pt;"><b>Security</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
SPA
are vulnerable to two important issues like Cross site Scripting(XSS) and
Cross Site Request Forgery(CSRF). Backbone does not have any security policy
regarding these. Its even does not offer any documention on how to write
secure code using this library. The templates, routers being used in backbone
is highly vulnerable to attacks. </div>
</td>
</tr>
<tr height="40" style="height: 30.0pt;">
<td class="xl66" height="40" style="border-top: none; height: 30.0pt;"><b>Learning curve</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">Very
easy to learn it. Huge amount of online materials, examples and good quality
of documention is present.</td>
</tr>
<tr height="60" style="height: 45.0pt;">
<td class="xl66" height="60" style="border-top: none; height: 45.0pt;"><b>Object
Decoupling</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Backbone
does not provide any builtin support for object decoupling. But, we can apply
it by using custom pub/sub pattern or to some extent by using event
machanism.</div>
</td>
</tr>
<tr height="100" style="height: 75.0pt;">
<td class="xl66" height="100" style="border-top: none; height: 75.0pt;"><b>Routing</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Backbone.Router
provides methods for routing client-side pages, and connecting them to
actions and events. The router class must contain an url or route and a
function which will be triggered if that route or url matches. It is also
possible to create dynamic router depending on the parameters being sent with
the url.</div>
</td>
</tr>
<tr height="140" style="height: 105.0pt;">
<td class="xl66" height="140" style="border-top: none; height: 105.0pt;"><b>Template</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513"><div style="text-align: justify;">
Although
templating is not directly provided by backbone, yet it is strongly recmonded
for creating strong and reusable UI design. Backbone uses Underscore.js i.e.
_.template() to create template. It is suggested to choose a nice JavaScript
templating library. Mustache.js, Haml-js, and Eco are all fine alternatives.
Because Underscore.js is already on the page, _.template is available, and is
an excellent choice if you prefer simple interpolated-JavaScript style
templates</div>
</td>
</tr>
<tr height="20" style="height: 15.0pt;">
<td class="xl66" height="20" style="border-top: none; height: 15.0pt;"><b>Minification</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">Does
not support directly as provided by Extjs using Sencha CMD. Done by using either Require.js or uglyfy.js or others</td>
</tr>
<tr height="20" style="height: 15.0pt;">
<td class="xl66" height="20" style="border-top: none; height: 15.0pt;"><b>AMD Support</b></td>
<td class="xl67" style="border-left: none; border-top: none; width: 385pt;" width="513">AMD
is supported in backbone projects with the help of Require.js. </td>
</tr>
</tbody></table>
<br />
<div style="text-align: justify;">
So, from above discussion, it is clear that Backbone is not such a framework that provides you everything in a single roof. It just provides handling and management of Model, views, template with the help of underscore.js, asynchronous module loading although with the help of Require.js. It facilitate app development by abstracting several boilerplate code. It does not force developer to follow its hard core syntax but help them to do some unique by using its helping hand.</div>
<div style="text-align: justify;">
<br /></div>
<div>
<div style="text-align: justify;">
There are several plugins designed in backbone like Marionette, Chaplin, rendr and several others. The list of all plugins can be found on <b>http://backplug.io/</b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
One thing that I want to make clear that from above discussion my intent is not to show that which framework is more powerful as each framework comes with some unique features and can't fit in all scenarios. So, above discussion may be helpful in selecting a framework suitable for an application evaluated on above criteria.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Guys, please feel free to send your response regarding the above discussion and your suggestions regarding adding any new criteria in this discussion is most welcome.</div>
</div>
<br /><br />
<div>
Very important links related to this post:</div>
<div>
<ul style="text-align: left;">
<li><a href="http://jksnu.blogspot.in/" target="_blank"><span style="color: red;">Collection of my posts</span></a></li>
</ul>
</div><br /><br />
</div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0tag:blogger.com,1999:blog-3597209610268531963.post-51470545104697199122014-09-08T07:18:00.002-07:002014-09-08T07:37:11.055-07:00Steps to create new plugin for BingMap by Eclipse in Microstrategy Perspective<div dir="ltr" style="text-align: left;" trbidi="on">
Very important links related to this post:
<br />
<ul style="text-align: left;">
<li><a href="http://jksnu.blogspot.in/" target="_blank"><span style="color: red;">Collection of my posts</span></a></li>
<li><a href="http://jksnu.blogspot.in/2014/08/steps-to-configure-microstrategy-with.html" target="_blank"><span style="color: red;">Steps to configure Microstrategy with Eclipse</span></a></li>
</ul>
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
In my previous post, I have discussed the "<a href="http://jksnu.blogspot.in/2014/08/steps-to-configure-microstrategy-with.html" target="_blank">Steps to configure Microstrategy with Eclipse</a>". In this post, I am going to reveal the steps for creating the plugins for BingMap by Eclipse in Microstrategy Perspective.<br />
<br />
<b>Step1</b>: Open Eclipse and go to the Microstartegy perspective and click on Select plugin icon in Application Settings tab.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifzIBnYGiUiqCqaQ76A1XYPIOD2v1Q4TowGLzf53zEKc3b9djgnaEJcxZz6ZkE-82WwQRwB4bTK4mQfPIXtajqljspi6oc5Ejo1oPwL2fqVfO4vlb0szC7MFIM0CD2Grw8oeYBcK0PItY/s1600/step1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifzIBnYGiUiqCqaQ76A1XYPIOD2v1Q4TowGLzf53zEKc3b9djgnaEJcxZz6ZkE-82WwQRwB4bTK4mQfPIXtajqljspi6oc5Ejo1oPwL2fqVfO4vlb0szC7MFIM0CD2Grw8oeYBcK0PItY/s1600/step1.png" height="251" width="640" /></a></div>
<br />
<b>Step2</b>: Type new plugin name and click on ok.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLA1UySTMWQjLErlVsjAZ1GybRztgpKGI8NgwmWjXXddDnq6OKXsWGEfQzgnsVRrmGTpGEAp8JhIAc1khn8bmdBKL4q4_3Waiy4lgQeZI8SGNbXj7VP_6At9w5eF2K3oZHWamG30k-298/s1600/step2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLA1UySTMWQjLErlVsjAZ1GybRztgpKGI8NgwmWjXXddDnq6OKXsWGEfQzgnsVRrmGTpGEAp8JhIAc1khn8bmdBKL4q4_3Waiy4lgQeZI8SGNbXj7VP_6At9w5eF2K3oZHWamG30k-298/s1600/step2.png" height="224" width="320" /></a></div>
<br />
<b>Step3</b>: After this, the new plugin will be created at following location:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt89UGCmvWbkkZ3bRJS9G0SH2Uc9GW5OjcVtImz86EMqeBKk-4PbuegmCz39t-pfylUJ3e6wnStI8EGxR0AoRWg_VMVEqjB2-PUcWPkbuVzxAuUOD2F64un5j2G5e88WYib6JxH6HrkDQ/s1600/step3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt89UGCmvWbkkZ3bRJS9G0SH2Uc9GW5OjcVtImz86EMqeBKk-4PbuegmCz39t-pfylUJ3e6wnStI8EGxR0AoRWg_VMVEqjB2-PUcWPkbuVzxAuUOD2F64un5j2G5e88WYib6JxH6HrkDQ/s1600/step3.png" height="400" width="300" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<b>Step4</b>: Create new style for each of the 3 java files by clicking on new Style under Application settings tab.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl8nRkKqoN1Qig1DkQDWzWByuW4cuAI5MPUe7vMrWolhoH1b69zc1S5c3OVhy6uXgTvd1Ke-hOoEzdg53GGn5LfqoUdgpxSf_ZIIi2TbnXc2C6KnRv8n1ebGCZrAUv6EzWc00YH9Vbuvs/s1600/step4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl8nRkKqoN1Qig1DkQDWzWByuW4cuAI5MPUe7vMrWolhoH1b69zc1S5c3OVhy6uXgTvd1Ke-hOoEzdg53GGn5LfqoUdgpxSf_ZIIi2TbnXc2C6KnRv8n1ebGCZrAUv6EzWc00YH9Vbuvs/s1600/step4.png" height="400" width="375" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<b>Step5</b>: Fill the wizard as shown in image.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZg360s8rL-TtAZzrL0OU17OYblxFS_MFUHtjE1zG9tTaYEJmT8T9QjfpYH9ScHMCEaCyzIgX-4JV9nUGb9XBNkgzhtDA0QLlTDOmATH8SoBcEJpxxkKmjX6520iJrkxHAXnAqaqSrQzI/s1600/step5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZg360s8rL-TtAZzrL0OU17OYblxFS_MFUHtjE1zG9tTaYEJmT8T9QjfpYH9ScHMCEaCyzIgX-4JV9nUGb9XBNkgzhtDA0QLlTDOmATH8SoBcEJpxxkKmjX6520iJrkxHAXnAqaqSrQzI/s1600/step5.png" height="400" width="393" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<b>Step6</b>: Establish relation between [style 2 and style1 ] and [style 3 and style 2]. For this select style2 and style parameter tab at the bottom.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAMs08A8_KLcIiSDutt8SW9WZVmanro1ydFUFHffWWkuEzQOSp5l1TrY9syixKIVcihRxK-GW7HdbZJKMWxhMB11BVbbb0blTgRxVQzo_O-sj6bZZYicTIp6xth9ewiqZP8wBoNyLFcbU/s1600/step6.1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAMs08A8_KLcIiSDutt8SW9WZVmanro1ydFUFHffWWkuEzQOSp5l1TrY9syixKIVcihRxK-GW7HdbZJKMWxhMB11BVbbb0blTgRxVQzo_O-sj6bZZYicTIp6xth9ewiqZP8wBoNyLFcbU/s1600/step6.1.png" height="400" width="393" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif5QulRgr8bc3bEHcefFDgfxb7fBGuOlO4iZA5F8lvXZ90zl6lTGefZdw5iqjEhOI-YVpn43zv4vI7F7v1EM0PCjLzZSuzOh0SYlrXyY_h8plyoNE9oVHS16AiNA6nNTOBSQxVcloEcNY/s1600/step6.2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif5QulRgr8bc3bEHcefFDgfxb7fBGuOlO4iZA5F8lvXZ90zl6lTGefZdw5iqjEhOI-YVpn43zv4vI7F7v1EM0PCjLzZSuzOh0SYlrXyY_h8plyoNE9oVHS16AiNA6nNTOBSQxVcloEcNY/s1600/step6.2.png" height="433" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqrztu2-4vlyXZtM82u0gz7NCucW6p5K2_BHa2jUQj7Q9gS3qQwCqqWzDHw9pJYH4R1LMQ1i5rIOJXieQsafoyNhYPYr4VPQLVK5DJuw0LGWvq4KHb-JiLcYlqqRcBQ_gK6bGg0LjkqKw/s1600/step6.3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqrztu2-4vlyXZtM82u0gz7NCucW6p5K2_BHa2jUQj7Q9gS3qQwCqqWzDHw9pJYH4R1LMQ1i5rIOJXieQsafoyNhYPYr4VPQLVK5DJuw0LGWvq4KHb-JiLcYlqqRcBQ_gK6bGg0LjkqKw/s1600/step6.3.png" height="284" width="640" /></a></div>
<br />
<b>Step7</b>: Now map layout.xml with Style2. for this, select style2 and click on style layouts tab at the bottom and select the path of already existing layout.xml file.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7NRjHkpGYxgvPfyMYg6ZqZ2oc2X-UaIh_UNcWf27LmPlU68UulqvDY4CjVTs1s5rGY2OgZ5b_hyMxcOqbbNIIREag2c6u40nMr7bLUqMnsXzJ2Zw-vs6exB28nRTN0qBdA9ELtjWoexE/s1600/step7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7NRjHkpGYxgvPfyMYg6ZqZ2oc2X-UaIh_UNcWf27LmPlU68UulqvDY4CjVTs1s5rGY2OgZ5b_hyMxcOqbbNIIREag2c6u40nMr7bLUqMnsXzJ2Zw-vs6exB28nRTN0qBdA9ELtjWoexE/s1600/step7.png" height="384" width="640" /></a></div>
After these steps, our plugin will be ready to test and it will be automatically deployed in Microstrategy folder in Tomcat folder which was generated due to exploding of Microstrategy.war on tomcat start. Now, we can do most of the visual work by doing some changes in layout.xml file. The Layout.xml file is an xml file having some javascript code inside it. We can easily modify the javascript code here to get visual appearance as per our requirement. We can easily debug the javascript code inside this xml file by using Chrome debugger tool.<br />
<br />
Very important links related to this post:<br />
<ul style="text-align: left;">
<li><a href="http://jksnu.blogspot.in/" target="_blank"><span style="color: red;">Collection of my posts</span></a></li>
<li><a href="http://jksnu.blogspot.in/2014/08/steps-to-configure-microstrategy-with.html" target="_blank"><span style="color: red;">Steps to configure Microstrategy with Eclipse</span></a></li>
</ul>
<br />
</div>
</div>Jitendrahttp://www.blogger.com/profile/13070988181879647919noreply@blogger.com0