Unit testing nodejs with Mocha and Chai

There are a lot of unit testing frameworks available for Javascript, Jasmine and Karma being some of the older and more popular ones. Jasmine and Karma, though are, originally designed for browser-side JS, and hence, frameworks like NodeUnit and Mocha have become more popular with server-size JS. We needed code coverage reports to work after the unit tests, and the Jasmine-Node reports were not sufficient, so we just moved to using Mocha. When using Mocha, we can use some assert library (which is not necessary, but makes life a hell lot easier). We are using chai at the open-event-webapp.. First of all install mocha globally - npm install -g mocha And write your tests in the test/ folder that mocha by default considers as the folder containing your test specs. For example we have our tests here - https://github.com/fossasia/open-event-webapp/tree/master/test Writing a simple mocha test is as easy as this - var assert = require('chai').assert; describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.equal(-1, [1,2,3].indexOf(5)); assert.equal(-1, [1,2,3].indexOf(0)); }); }); }); The first parameter inside describe() is just to show the tests in a aesthetic way in the console. You can see our tests described in this file  - https://github.com/fossasia/open-event-webapp/blob/master/test/serverTest.js And attached below is an screenshot of the terminal after I have run the command mocha in the root of my project

Continue ReadingUnit testing nodejs with Mocha and Chai

Debugging with Node

Nodejs is a powerful Event-driven I/O server-side JavaScript environment based on V8. Debugging of Node applications is not as easy as the browser code. But the node wonderful debugger can make it easy if it is used effectively. How to start the debugger For debugging node applications, we have to only run a debug command. Here for a quick demonstration, I have used Open-Event-Webapp fold.js code. Suppose there is a problem in the script file and we have to debug it. All we have to do is to run the debug command from the directory containing the file. node debug ./fold.js The output screen will show something like this: This brings us into the debug mode. When we are in debug mode, we can try various commands like 'n' for next, 's' for the step into a function, 'list(k)' where k is the number of lines of the code you want on the screen. The 'n' command always takes us to next instruction, hence in long codebases, it is always recommended to use 'c' or Continue Execution command for going to the next breakpoint. To set the breakpoint, we can use the command: setBreakpoint() or sb() The snapshot shows the output once the breakpoint is set. We can check the values at the breakpoint by using command repl. Debugging the code correctly can save a lot of time and effort. These techniques provided by the debugger are necessary to learn. Alternates node-inspector iron-node

Continue ReadingDebugging with Node

sTeam REST API

(ˢᵒᶜⁱᵉᵗʸserver) aims to be a platform for developing collaborative applications. sTeam server project repository: sTeam. sTeam-REST API repository: sTeam-REST REST Services REST is the software architectural style of the World Wide Web. REST (Representational State Transfer) was introduced by Roy Fielding in his doctoral dissertation in 2000. Its purpose is to induce performance, scalability, simplicity, modifiability, visibility, portability, and reliability.It has client/server relationship with a uniform interface and is stateless. REST is most commonly associated with HTTP but it is not strictly related to it. REST Principles Resources : Each and every component is a resource.A resource is accessed by a common interface using HTTP standard methods. Messages use HTTP methods like GET, POST, PUT, and DELETE. Resource identification through URI: Resources are identified using URI. Resources are represented using JSON or XML. Stateless interactions take place between the server and the client. No context is saved for the requests at the server.The client maintains the state of the session. HTTP methods The CRUD(create, retrieve, update and delete ) operations are performed using the HTTP methods. GET It is used to retrieve information. GET requests executed any number of times with the same parameters, the results would not change. This makes it idempotent. Partial or conditional requests can be sent. It is a read only type of operation. Retrieve a list of users: GET /api.example.com/UserService/users POST POST is usually used to create a new entity. It can also be used to update an existing entity. The request will have to do something with the entity provided in the URI. Create a new user with an ID 2: POST /api.example.com/UserService/users/2 PUT PUT request is always idempotent. Executing the same request any number of times will not change the output. PUT can be used to create or update an existing entity. Modify the user with an ID of 1: PUT /api.example.com/UserService/users/1 PATCH It is idempotent. PATCH requests only updates the specified fields of an entity. Modify a user with an id of 1: PATCH /api.example.com/UserService/users/1 DELETE It can be asynchronous or a long-running request. It removes the resource. It can be removed immediately or at a later instance of time. Delete a user with an ID of 1: DELETE /api.example.com/UserService/users/1  sTeam-REST API Installing and activating the REST API The REST API is developed as an application inside the sTeam server. This simplifies development quite a lot, as we don't need to restart the server for every change. Instead just the API code gets updated and reloaded. It may eventually be integrated into the core, however the longterm plan is actually to move functionality out of the core, to make development easier. To get the current version of the API clone the steam-rest repo into your home or to any place where you keep your development repos. Then change to the tools directory of your installation and run import-from-git. git clone https://github.com/societyserver/steam-rest cd steam-rest git checkout origin/rest.pike export steamrest=`pwd` cd /usr/local/lib/steam/tools ./import-from-git.pike -u root $steamrest / Note: The new import-from-git.pike script supports importing…

Continue ReadingsTeam REST API

Sending a streaming zip file in node.js

The webapp generator that we set up is now up and running on a heroku instance and is working to generate zips of generated websites. One of the requirements was that when the front-end presses, the "Generate" button, a request is created to the backend node app to start creating the html pages, and pack them into a zip file. Then the said zip file will be made available to download. I intended for the zip file to become available to download immediately and the server to continuously pack data into the zip, as the user downloads it - a process known as streaming zip serving. (If you might have noticed, Google Drive uses this too. You do not know the final zip size before the download has completed.) If the server takes time in creating the contents of the zip, this method can help, as it will allow the user to start downloading the file, before all the contents are finalised.   To achieve the mentioned goal, the idea Node.js module to use archiver. If you look at the app.js code you'll find on the /generate POST endpoint, we take the 'req' params and pass to a pipeToRes() function, which is defined in generator.js app.post('/generate', uploadedFiles, function(req, res, next) { console.log(req.files); console.log(req.body); res.setHeader('Content-Type', 'application/zip'); generator.pipeZipToRes(req, res); }); And in the generator, you can find this block of code that pipes the streaming archive to the response.   const zipfile = archiver('zip'); zipfile.on('error', (err) => { throw err; }); zipfile.pipe(res); zipfile.directory(distHelper.distPath, '/').finalize(); We zip the directory defined in distPath and the archiver API has a fluent call to .finalize() that finished the download for the user, when the zip is completed.

Continue ReadingSending a streaming zip file in node.js