Post

Week #4 challenge: Server-side JavaScript

This week on my 52 weeks, 52 projects challenge I tried a back-end JavaScript framework called Sails.js. Sails is an MVC framework based on Express.js and socket.io that resembles the MVC behavior of frameworks like Ruby on Rails. I also used MongoDB, a NoSQL database, to store the data.

You’ve probably heard before that it is possible to use JavaScript on the server-side just like you use Rails, Spring, PHP, etc. This is possible because there are projects like Node.js that allow the developer use JavaScript to run in the backend of a web service. Node.js uses a non-blocking, event-driven I/O model that makes your web application/service lightweight and efficient.

Using Sails it’s not the only way to develop Node.js applications. In fact, there’s a full-stack JavaScript solution called MEAN, which stands for Mongo, Express.js, Angular.js and Node.js. Since MongoDB actually stores data in the BSON (Binary JSON) format, you can say that the MEAN stack is a way of using JavaScript for (almost) everything in your web applictation. This week’s project is not a MEAN application: it doesn’t have the Angular part (yet). But since Sails uses Express.js under the cover and I used MongoDB, we can agree that it is a MEN application (I hope there isn’t any feminist unfollowing me for that).

During this week I created a demo API using Sails. It is not something very special to be done during a week, but I didn’t really work too much on it. During the past weeks I used to work 3 hours/day on the projects, but this week wasn’t the best. One or two days I couldn’t work at all on this. Anyway, it gives the basic idea of building a simple API in Sails. It handles five routes, all sharing the base api/v1/ship route:

MethodRouteDescription
GETapi/v1/ship/Get the list of all ships stored
GETapi/v1/ship/:idGet the data for a single ship with the ID of id
POSTapi/v1/ship/Insert a ship in the database
PUTapi/v1/ship/:idUpdate data for a ship with the given ID of id
DELETEapi/v1/ship/:idDelete the ship with ID of id

The data is stored in a Mongo database. Sails comes with a simple-to-use ORM/ODM called Waterline, which makes it very simple to use any kind of database management system, both SQL and NoSQL. If you need to change the DB management system, all you have to do is install the proper database driver, change the configurations file, and that’s it! The interface that Waterline gives to you is the same and DB-independent, so the rest of the code remains the same.

Unfortunately, I didn’t add any kind of validation or authentication/authorization mechanism, which is not cool. Next week, I intend to add such things, both a validation mechanism (so the data posted is the right one) and an authentication/authorization mechanism (so not all users can delete and/or update data). Maybe in a later time, I’ll use the same application for playing with some Android development, probably using Reactive Extensions for Java.

This is for the 4th week. If you want to check the application, you can find it on GitHub with other information on how to run the application. I included the exported data from MongoDB, so you can use the same data if you want. Also I added some shell scripts that I used to test POST-ing, PUT-ing and DELETE-ing.

If you’re interested in learning more about the MEAN stack, you can read this Introduction to the MEAN Stack or check out my Intro to MEAN presentation.

This post is licensed under CC BY 4.0 by the author.