<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ketav Chotaliya on Medium]]></title>
        <description><![CDATA[Stories by Ketav Chotaliya on Medium]]></description>
        <link>https://medium.com/@chotaliyaketav?source=rss-6d128e0f6da9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*v-3HRSD-CGXfBMoWPc5Dgw.jpeg</url>
            <title>Stories by Ketav Chotaliya on Medium</title>
            <link>https://medium.com/@chotaliyaketav?source=rss-6d128e0f6da9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 16:04:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chotaliyaketav/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unit Testing with Mocha and Chain in Node JS]]></title>
            <link>https://medium.com/@chotaliyaketav/unit-testing-with-mocha-and-chain-in-node-js-864f85b25ebc?source=rss-6d128e0f6da9------2</link>
            <guid isPermaLink="false">https://medium.com/p/864f85b25ebc</guid>
            <category><![CDATA[mocha-js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[chaijs]]></category>
            <category><![CDATA[sinonjs]]></category>
            <dc:creator><![CDATA[Ketav Chotaliya]]></dc:creator>
            <pubDate>Mon, 06 May 2019 06:59:43 GMT</pubDate>
            <atom:updated>2019-05-06T09:01:13.072Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is unit testing?</strong></p><p>Unit testing is a process to test a smallest or individual part of the software. We can say a unit test is a method to validate each unit (part) of software individual.</p><p><strong>Why unit testing is important?</strong></p><p>Unit testing is about validating each individual function of software with various test cases. So, we can assure that function (or particular part) is working properly with mentioned test cases. So, the overall time of software development will reduce.</p><p><strong>How to do unit testing?</strong></p><p>In Node JS, we can do unit testing using <a href="https://mochajs.org/"><strong>mocha.js</strong></a><strong> </strong>and <a href="https://www.chaijs.com/"><strong>chai.js</strong></a><strong> </strong>frameworks<strong>. </strong>We can use another javascript framework (like <a href="https://sinonjs.org/"><strong>sinon.js</strong></a><strong>) </strong>for more features like Spy or mock the dependencies.</p><p><strong>What is Mocha, Chai and Sinon JS?</strong></p><ul><li><strong>M</strong><a href="https://mochajs.org/"><strong>ocha.js</strong></a><strong> </strong>is a javascript test runner framework.</li><li><strong>C</strong><a href="https://www.chaijs.com/"><strong>hai.js</strong></a><strong> </strong>is a javascript test framework<strong>.</strong></li><li><a href="https://sinonjs.org/"><strong>sinon.js</strong></a><strong> </strong>provides standalone test spies, stubs and mocks.</li></ul><p><strong>Different Practices of Unit Testing</strong></p><ul><li>TDD (Test Driven Development)</li><li>BDD (Behavior Driven Development)</li></ul><p><strong>What’s the difference between TDD &amp; BDD?</strong></p><ul><li>In TDD (Test Driven Development), the test is written to check the implementation of functionality, but as the code evolves, tests can give false results.</li><li>BDD (Behavior Driven Development) is also a test-first approach but differs by testing the actual behaviour of the system from the end users perspective.</li></ul><p><strong>Let’s start unit testing step by step</strong></p><p><strong><em>Step 1: Install the dependencies</em></strong></p><pre><em>npm install chai --save-dev</em></pre><pre><em>npm install chai-http --save-dev</em></pre><pre><em>npm install mocha --save-dev</em></pre><p>install ‘sinon.js’ package for mock the function (dependencies).</p><pre><em>npm install </em>sinon<em> --save-dev</em></pre><p>install ‘mochawesome’ package for the unit test report.</p><pre><em>npm install mochawesome --save-dev</em></pre><p><strong><em>Step 2: Setup the directory structure and test config file</em></strong></p><p>Create a config file for the test environment</p><pre><em>mkdir utils</em></pre><pre><em>cd utils</em></pre><pre><em>touch test_config.js</em></pre><p>Define config variables in the <strong>test_config.js</strong> file</p><pre><em>&#39;use strict&#39;;</em></pre><pre><em>module.exports = {<br>    consulUrl: &#39;http://localhost:8080&#39;,<br>    consulPort: &#39;8080&#39;,<br>    studentApiUri: &#39;/students/api/v1/&#39;,<br>    studentPublicApiUri: &#39;/students/public/api/v1/&#39;,<br>    dummyAuthorizationToken: &#39;Bearer qiwueey879kjhqawsd.kdjhfqacj&#39;,<br>    dummyText: &#39;Lorem Ipsum is simply dummy text.&#39;,<br>    dummyUUID: &#39;123456789-123456789-123456789-123456789-12&#39;<br>}</em></pre><p><strong><em>Step 3: </em>Create a helper file for the test environment which contains common functions.</strong></p><p>Create generic functions for unit testing in helper file like</p><ul><li>createStub()</li><li>restoreStub()</li><li>chaiRequest() (like GET, POST, PUT, DELETE)</li><li>checkValidOutput</li><li>And many others</li></ul><p><strong>Step 4: Create a spec file which contains test cases.</strong></p><pre>touch abcController.spec.js</pre><p>Before going to write test cases, Let’s understand basic syntax.</p><ul><li>describe() — Contain all the test cases</li><li>beforeEach() — Execute before every test case.</li><li>afterEach() — Execute after every test case.</li><li>before() — Execute once before every test cases run.</li><li>after() — Execute once after every test cases.</li><li>it() — Write individual test case here.</li></ul><p><strong>Code Example</strong></p><pre><em>&#39;use strict&#39;</em></pre><pre><em>const chai = require(&#39;chai&#39;);</em></pre><pre><em>const expect = chai.expect;</em></pre><pre><em>const testHelper = require(&#39;../../../utils/test_helper&#39;);</em></pre><pre><em>const testEnv = require(&#39;../../../utils/test_config&#39;);</em></pre><pre><em>const commonService = require(&#39;../services/commonService&#39;);</em></pre><pre><em>// Test cases for Business Operator modules</em></pre><pre><em>describe(&#39;Test cases for ABC Module&#39;, () =&gt; {</em></pre><pre><em>    // Test Cases for sendOTP API</em></pre><pre><em>    describe(&#39;Test Cases for sendOTP API&#39;, () =&gt; {</em></pre><pre><em>        let commonServiceStub;</em></pre><pre><em>        beforeEach(() =&gt; {</em></pre><pre><em>            commonServiceStub = testHelper.createStub(commonService, &#39;getUserDetail&#39;);</em></pre><pre><em>        });</em></pre><pre><em>        afterEach(() =&gt; {</em></pre><pre><em>            testHelper.restore()</em></pre><pre><em>        });<br>      <br>        // Create error in CommonService</em></pre><pre>        it(&#39;Create error in CommonService, It should return 500 Internal Error&#39;, async () =&gt; {</pre><pre>            commonServiceStub.onCall(0).callsFake(() =&gt; <em>Promise</em>.reject({</pre><pre>                status: 500,</pre><pre>                stack: &#39;Error Stack&#39;,</pre><pre>                message: &#39;Error message goes here&#39;,</pre><pre>                payload: {},</pre><pre>                pager: {}</pre><pre>        }));</pre><pre>            let res = await testHelper.chaiPostRequest(testEnv.studentApiUri + &#39;students/sendOTP&#39;, {});</pre><pre>            testHelper.checkValidOutput(res);</pre><pre>            expect(res).to.have.status(500);</pre><pre>            expect(res.body.message).to.eql(&#39;Error message goes here&#39;);</pre><pre>        })</pre><pre>})</pre><p><strong>How to run test cases?</strong></p><p>Add below command in a package.json file inside the <em>script </em>tag.</p><p><strong>“test”</strong>: <strong>“mocha — recursive — reporter mochawesome spec app/**/*.spec.js — timeout 15000”</strong></p><p>Run “<strong><em>npm test” </em></strong>command to run all the test cases</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=864f85b25ebc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REST API with Node JS using MySQL]]></title>
            <link>https://medium.com/@chotaliyaketav/rest-api-with-node-js-using-mysql-87f30f9dfd80?source=rss-6d128e0f6da9------2</link>
            <guid isPermaLink="false">https://medium.com/p/87f30f9dfd80</guid>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Ketav Chotaliya]]></dc:creator>
            <pubDate>Tue, 05 Jun 2018 10:26:03 GMT</pubDate>
            <atom:updated>2019-03-11T10:08:41.562Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a very beginning of nodeJS who want to learn from scratch. Below are the steps to create an application with Node JS using Express JS.</p><p>Express JS is a web application framework for Node JS.</p><p>Let’s begin.</p><ol><li>Verify that Is node js installed or not using below command.</li></ol><pre><strong>nodejs -v</strong></pre><p>It will display version of node js installed in your system, If it not displaying version then you need to install node js in your system.</p><p>Install NodeJS using below command</p><pre><strong>sudo</strong> <strong>apt</strong>-<strong><em>get update</em></strong></pre><pre><strong>sudo</strong> <strong>apt</strong>-<strong><em>get </em>install nodejs</strong></pre><pre><strong>nodejs -v</strong></pre><p>2. You have to verify that is NPM (Node Package Manager) available in your system or not using below command.</p><pre><strong>npm -v</strong></pre><p>It will display version of NPM in your system, If it is not displaying version then you need to install NPM in your system.</p><p>Install NPM using below command.</p><pre><strong>sudo</strong> <strong>apt</strong>-<strong><em>get update</em></strong></pre><pre><strong>sudo apt-get install npm</strong></pre><pre><strong>npm -v</strong></pre><p>3. It’s good to go now. Go to your prefered work directory and create project-directory and go inside project-directory.</p><pre><strong>mkdir &lt;project-directory&gt;</strong></pre><pre><strong>cd &lt;project-directory&gt;</strong></pre><p>4. Initiate the Node JS using <strong>npm init</strong>, It will create package.json file in your project-directory.</p><p>package.json file will contain all the project related information and dependency packages.</p><pre><strong>npm init</strong></pre><p>5. Now you have to install dependencies in your project. Some of the dependencies are common for all. You can add/remove dependencies with your requirements.</p><pre><strong>npm install --save express<br>npm install --save body-parser<br>npm install --save bcrypt<br>npm install --save express-validator<br>npm install --save jsonwebtoken<br>npm install --save mysql<br>npm install --save util<br>npm install --save fs<br>npm install --save express-fileupload</strong></pre><p>You can add some devDependencies also</p><pre><strong>npm install --save-dev nodemon</strong></pre><p>It will create one directory named <strong>node_modules </strong>which contain all the dependencies.</p><p>6. Create an entry point of the project. Create an <em>index.js</em> file in the root of the project directory</p><pre><strong>touch index.js</strong></pre><p>7. Import the dependencies into index.js file</p><pre><strong>express         </strong>= module.<strong>exports </strong>= require(<strong>&#39;express&#39;</strong>);<br><strong>bodyParser      </strong>= module.<strong>exports </strong>= require(<strong>&#39;body-parser&#39;</strong>);<br><strong>expressValidator</strong>= module.<strong>exports </strong>= require(<strong>&#39;express-validator&#39;</strong>);<br><strong>fileUpload      </strong>= module.<strong>exports </strong>= require(<strong>&#39;express-fileupload&#39;</strong>);<br><strong>jwt             </strong>= module.<strong>exports </strong>= require(<strong>&#39;jsonwebtoken&#39;</strong>);<br><strong>fs              </strong>= module.<strong>exports </strong>= require(<strong>&#39;fs&#39;</strong>);<br><strong>let </strong>route      = require(<strong>&#39;./routes&#39;</strong>); //path to the routes.js<br><strong>connection      </strong>= require(<strong>&#39;./config/database&#39;</strong>) //path to the config/database.js</pre><p><em>module.exports</em> will export these package across other files.</p><p>Set the port number for our application in which all request will be serve. This application will be run on 8080 port of HTTP.</p><pre><strong>var </strong>port = process.<strong>env</strong>.PORT || 8080;</pre><p>8. create an instance of express and use dependencies in the app.</p><pre><strong>var app = express();<br>app.use(bodyParser.urlencoded({extended: false}))<em><br></em>app.use(bodyParser.json());<br>app.use(expressValidator());</strong><br><br><strong>app.use(&#39;/v1.0&#39;,route);</strong> //Set prefix in route v1.0; redirect the <br>routes to the routes.js file; </pre><pre><em>// user routes<br></em><strong>app.use(&#39;/v1.0/user&#39;,userRoutes);</strong></pre><pre>//create app listner<br>app.listen(port, <strong><em>console</em></strong>.log(<strong>&#39;Application is running at &#39; </strong>+ port));</pre><pre>//export the instance of express<br>module.<strong>exports </strong>= app;</pre><p>9. Create a routes.js file in the root of project-directory</p><pre><strong>touch routes.js</strong></pre><p>10. Create an instance of express routes</p><pre><strong>var routes = express.Router(); </strong>//create instance of express routes</pre><pre>routes.get(<strong>&#39;/health&#39;</strong>, (req, res, next) =&gt; {<br>    res.<strong>status</strong>(200).json({ <strong>success</strong>: <strong>true </strong>})<br>});</pre><pre><strong>module.exports = routes; </strong>//export express routes</pre><p>11. Create a user.route.js file inside <em>routes/</em> directory</p><pre><strong>mkdir routes<br>cd routes<br>touch user.route.js</strong></pre><p>12. user.route.js file</p><pre><strong>var routes = express.Router(); </strong>//create instance of express routes</pre><pre><strong>var userValidator = require(&#39;./../validation/user.validation&#39;); </strong>//path to the validation/user.validation.js</pre><pre><strong>var userController = require(&#39;./../controller/user.controller&#39;); </strong>//path to the controller/user.controller.js<br></pre><pre>//create the routes</pre><pre><strong>routes.post(&#39;/&lt;your-route-name&gt;&#39;,<br>    [<br>      userValidator.validateAddUser, </strong>//function that validate request using express-validator<strong><br>      userValidator.checkValidationResult,<br>    ],<br>    userController.createUser </strong>//controller function that actually perform business logic<strong><br>);</strong></pre><p>13. Create user.validation.js file inside <em>validation/</em> directory</p><pre><strong>mkdir ../validation<br>cd validation<br>touch user.validation.js</strong></pre><p>14. user.validation.js file</p><pre><strong>var response = require(&#39;./../general/MyResponse&#39;);</strong> //common function for every http response inside general folder</pre><pre><strong>var messages = require(&#39;./../general/messages&#39;);</strong> //common files inside general folder for each error message in app</pre><pre>//create instance of express-validator<br><strong>const {check,validationResult} = require(&#39;express-validator/check&#39;);</strong></pre><pre><strong>const {<em>matchedData</em>, sanitize} = require(&#39;express-validator/filter&#39;);</strong></pre><pre><strong>module.exports.<em>checkValidationResult </em>= <em>checkValidationResult</em>; </strong></pre><pre>//function that validate express-validator results<br><strong>function <em>checkValidationResult</em>(req, res, next) {<br>    var result = validationResult(req)<br>    if (!result.isEmpty()) {<br>        response.createResponse(res, 400,result.array()[0].msg,{}, {}); </strong>//response.createResponse is a method of general/MyResponse<strong><br>    } else {<br>        next(); </strong>//go ahead if request is valid<strong><br>    }<br>}</strong></pre><pre>//function that actually validate request<br>module.<strong>exports</strong>.<strong>validateAddUser </strong>= [<br>    check(<strong>&#39;email&#39;</strong>).isLength({<strong>min</strong>: 1}).withMessage(<strong>messages</strong>.<strong>EMAIL_REQUIRED</strong>).isEmail().withMessage(<strong>messages</strong>.<strong>INVALID_EMAIL</strong>),<br>    check(<strong>&#39;password&#39;</strong>).isLength({ <strong>min</strong>: 1 }).withMessage(<strong>messages</strong>.<strong>PASSWORD_REQUIRED</strong>),<br>    check(<strong>&#39;confirmPassword&#39;</strong>).isLength({ <strong>min</strong>: 1 }).withMessage(<strong>messages</strong>.<strong>CONFIRM_PASSWORD_REQUIRED</strong>).isLength({<strong>max</strong>:6}).withMessage(<strong>messages</strong>.<strong>CONFIRM_PASSWORD_MAX_LENGTH</strong>)<br>        .custom((value, {req}) =&gt; {<br>            <strong>if </strong>(value !== req.<strong>body</strong>.newPassword) {<br>                <strong>throw new <em>Error</em></strong>(<strong>messages</strong>.<strong>PASSWOD_AND_CONFIRM_PASSWORD_SAME</strong>)<br>            } <strong>else </strong>{<br>                <strong>return true</strong>;<br>            }<br>        }),<br>];</pre><p>15. Create user.controller.js file inside <em>controller/</em> directory</p><pre><strong>mkdir ../controller<br>cd controller<br>touch user.controller.js</strong></pre><p>16. user.controller.js file</p><p>Required necessary dependency</p><pre><strong>var </strong>response = require(<strong>&#39;./../general/MyResponse&#39;</strong>);<br><strong>var messages </strong>= require(<strong>&#39;./../general/messages&#39;</strong>);<br><strong>var </strong>logger = require(<strong>&#39;./../logs&#39;</strong>);<br><strong>var config </strong>= require(<strong>&#39;./../config/config&#39;</strong>);</pre><pre>module.<strong>exports </strong>= {<br>    createUser: <strong>async </strong>(req, res) =&gt; {<br>        <em>//create user<br>        </em><strong>var </strong>query = <strong>&quot;INSERT INTO users (&quot; </strong>+<br>            <strong>&quot;email, password ) &quot; </strong>+<br>            <strong>&quot;VALUES (?, ?)&quot;</strong>;<br><br>        <strong>var </strong>data = [req.<strong>body</strong>.<strong>email</strong>, req.<strong>body</strong>.<strong>password</strong>]<br>        <strong>try</strong>{<br>            <strong>var </strong>user = <strong>connection</strong>.query(query, data);<br>        } <strong>catch </strong>(err) {<br>            <strong><em>console</em></strong>.error(<strong>&quot;Error occurred : &quot;</strong>, err.<strong>message</strong>);<br>            response.<em>createResponse</em>(res,500,err.<strong>message</strong>,{},{});<br>        }<br>        response.<em>createResponse</em>(res, 200, <strong>messages</strong>.USER_CREATED_SUCCESS, {}, {});<br>    },<br>};</pre><p>17. create config folder for configuration and database connections.</p><pre><strong>mkdir ../config<br>cd config<br>touch config.js<br>touch database.js</strong></pre><p>18. config.js file</p><p>set all global variables inside this file</p><pre>module.<strong>exports </strong>= {<br>    <strong>superSecret </strong>: <strong>&#39;xyz@123&#39;</strong>,<br>    <strong>bcryptSalt </strong>: 10,<br>    <strong>dbHost </strong>: <strong>&#39;localhost&#39;</strong>,<br>    <strong>dbName </strong>: <strong>&#39;node-js-training&#39;</strong>,<br>    <strong>dbUser </strong>: <strong>&#39;root&#39;</strong>,<br>    <strong>dbPassword </strong>: <strong>&#39;root&#39;</strong>,<br>    <strong>EMAIL_USERNAME </strong>: <strong>&#39;abcd@xxx.com&#39;</strong>,<br>    <strong>EMAIL_PASSWORD </strong>: <strong>&#39;xxxxx&#39;</strong>,<br>};</pre><p>19. database.js file</p><pre><strong>var </strong>mysql = require(<strong>&#39;mysql&#39;</strong>); //require mysql<br><strong>var config </strong>= require(<strong>&#39;./config&#39;</strong>); //import config file for DB information<br><strong>var </strong>util = require(<strong>&#39;util&#39;</strong>);<br>//create pool connection of MySQL<br><strong>var </strong>pool = mysql.createPool({<br>    <strong>connectionLimit</strong>: 100,<br>    <strong>host</strong>: <strong>config</strong>.<strong>dbHost</strong>,<br>    <strong>user</strong>: <strong>config</strong>.<strong>dbUser</strong>,<br>    <strong>password</strong>: <strong>config</strong>.<strong>dbPassword</strong>,<br>    <strong>database</strong>: <strong>config</strong>.<strong>dbName<br></strong>});<br><br>pool.getConnection((err, connection) =&gt; {<br>    <strong>if </strong>(err) {<br>        <strong>if </strong>(err.<strong>code </strong>=== <strong>&#39;PROTOCOL_CONNECTION_LOST&#39;</strong>) {<br>            <strong><em>console</em></strong>.log(<strong>&#39;Database connection was closed.&#39;</strong>);<br>        }<br>        <strong>if </strong>(err.<strong>code </strong>=== <strong>&#39;ER_CON_COUNT_ERROR&#39;</strong>) {<br>            <strong><em>console</em></strong>.log(<strong>&#39;Database has too many connections.&#39;</strong>)<br>        }<br>        <strong>if </strong>(err.<strong>code </strong>=== <strong>&#39;ECONNREFUSED&#39;</strong>) {<br>            <strong><em>console</em></strong>.log(<strong>&#39;Database connection was refused.&#39;</strong>);<br>        }<br>        <strong>if </strong>(connection) connection.release();<br>        <strong>return<br>    </strong>}<br>});<br><br>pool.<strong>query </strong>= util.promisify(pool.<strong>query</strong>); <em>// Magic happens here.<br><br></em>module.<strong>exports </strong>= pool; //export the pool variable for globally use</pre><p>20. Create General files for common methods and error messages</p><pre><strong>mkdir ../general<br>cd general<br>touch messages.js<br>touch MyResponse.js</strong></pre><p>21. messages.js file</p><pre>module.<strong>exports </strong>= {<br>    <em>//Response messages</em><br>    <strong>SOMETHING_WRONG </strong>: <strong>&#39;Something went wrong. Please try later.&#39;</strong>,<br>    <strong>SERVER_ERR </strong>: <strong>&#39;Internal server error. Please try later.&#39;</strong>,<br><strong>    USER_CREATED_SUCCESS : &#39;User created successfully&#39;,<br></strong>};</pre><p>22. MyResponse.js file</p><pre><strong><em>exports</em></strong>.<em>createResponse </em>= <em>createResponse</em>; //export function for globally use</pre><pre><em>// Function for create response<br></em><strong>function </strong><em>createResponse</em>(res, status, message, data, pager)<br>{<br>    <strong>return </strong>res.status(status).send({<br>        <strong>&#39;status&#39; </strong>: status,<br>        <strong>&#39;message&#39; </strong>: message,<br>        <strong>&#39;payload&#39; </strong>: data,<br>        <strong>&#39;pager&#39; </strong>: pager<br>    });<br>}</pre><p>23. That’s it; Now you can index.js file directly or you can run it via <strong>nodemon </strong>also</p><pre><strong>node index.js</strong></pre><p>Or</p><pre><strong>nodemon</strong></pre><ul><li>You can refer <a href="https://github.com/ketavchotaliya/nodejs-boiler-plate">Github</a> link for the same.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*E8w2D7vscCvJPzuzh2Hbzw.png" /><figcaption>run application using nodemon</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*JbInsg9wD1skjTCOWxBYQg.png" /><figcaption>API response in JSON</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87f30f9dfd80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to send SMS using Twilio and PHP-Laravel]]></title>
            <link>https://medium.com/@chotaliyaketav/how-to-send-sms-using-twilio-and-php-laravel-1e80716180d6?source=rss-6d128e0f6da9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e80716180d6</guid>
            <category><![CDATA[sms-service]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[sms-api]]></category>
            <category><![CDATA[twilio]]></category>
            <category><![CDATA[sms]]></category>
            <dc:creator><![CDATA[Ketav Chotaliya]]></dc:creator>
            <pubDate>Mon, 12 Feb 2018 06:34:28 GMT</pubDate>
            <atom:updated>2019-02-13T17:49:20.196Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Twilio account</strong></p><ol><li>Go to <a href="http://www.twilio.com/">www.twilio.com</a> , Create new account</li></ol><ul><li>Twilio phone number</li></ul><p>2. Get a phone number(You will get one free twilio number for trial account)</p><ul><li>Twilio Account SID and Auth token.</li></ul><p><strong>NOTE : you may only use single phone number in your twilio trial account , and it is required that the number to which you send the sms from twilio should be verified by twilio.</strong></p><p>I am using laravel 5.4 for this demo application.</p><p>Steps are as follows:</p><ol><li>Create a new project in laravel.</li></ol><pre><strong><em>composer create</em></strong>-<strong><em>project laravel</em></strong>/<strong><em>laravel twilioSmsDemo</em></strong></pre><p>2. add the twilio sdk (using composer).</p><pre><strong><em>composer </em>require <em>twilio</em></strong>/<strong><em>sdk</em></strong></pre><p>3. Add these configuration settings of Twilio in your .<strong>env</strong> file.</p><pre>TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXX<br>TWILIO_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXX<br>TWILIO_APP_SID=APXXXXXXXXXXXXXXXXXXX</pre><p>4. Now add below code in <strong>config/app.php </strong>to access the twilio configuration variables globally.</p><pre><strong>&#39;twilio&#39; </strong>=&gt; [<br>    <strong>&#39;TWILIO_AUTH_TOKEN&#39;  </strong>=&gt; env(<strong>&#39;TWILIO_AUTH_TOKEN&#39;</strong>),<br>    <strong>&#39;TWILIO_ACCOUNT_SID&#39; </strong>=&gt; env(<strong>&#39;TWILIO_ACCOUNT_SID&#39;</strong>),<br>    <strong>&#39;TWILIO_APP_SID&#39;     </strong>=&gt; env(<strong>&#39;TWILIO_APP_SID&#39;</strong>)<br>],</pre><p>5. Now we will make one controller named <strong>SmsController</strong> to send sms using twilio.</p><pre><strong>&lt;?php<br><br>namespace </strong>App\Http\Controllers;<br><br><strong>use </strong>Illuminate\Http\Request;<br><strong>use </strong>Twilio\Rest\Client;<br><strong>use </strong>Twilio\Jwt\ClientToken;<br><br><strong>class </strong>SmsController <strong>extends </strong>Controller<br>{<br>    <strong>public function </strong>sendSms()<br>    {<br>        $accountSid = config(<strong>&#39;app.twilio&#39;</strong>)[<strong>&#39;TWILIO_ACCOUNT_SID&#39;</strong>];<br>        $authToken  = config(<strong>&#39;app.twilio&#39;</strong>)[<strong>&#39;TWILIO_AUTH_TOKEN&#39;</strong>];<br>        $appSid     = config(<strong>&#39;app.twilio&#39;</strong>)[<strong>&#39;TWILIO_APP_SID&#39;</strong>];<br>        $client = <strong>new </strong>Client($accountSid, $authToken);<br>        <strong>try<br>        </strong>{<br>            <em>// Use the client to do fun stuff like send text messages!<br>            </em>$client-&gt;<strong>messages</strong>-&gt;create(<br>            <em>// the number you&#39;d like to send the message to<br>                </em>&#39;+919033999999&#39;,<br>           <strong>array</strong>(<br>                 <em>// A Twilio phone number you purchased at twilio.com/console<br>                 </em><strong>&#39;from&#39; </strong>=&gt; <strong>&#39;+17204393482&#39;</strong>,<br>                 <em>// the body of the text message you&#39;d like to send<br>                 </em><strong>&#39;body&#39; </strong>=&gt; <strong>&#39;Hey Ketav! It’s good to see you after long time!&#39;<br>             </strong>)<br>         );<br>   }<br>        <strong>catch </strong>(Exception $e)<br>        {<br>            <strong>echo &quot;Error: &quot; </strong>. $e-&gt;getMessage();<br>        }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e80716180d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>