My first node.js project

I’ve been a PHP developer for the last 10 years and it has been a great, fun and defiant journey so far. I really like all I can do and did using this flexible and reliable language. It forced me to learn about many technologies around it and it opened important and interesting opportunities from a job perspective and a social one. I can’t complain: PHP made me meet great friends who which I now work with, it opened me the doors of new jobs perspectives and well, it made me grow professionally.

But here we are now, it is 2016 and a web site is no longer an apache, mysql, stylesheets and html render on the server. Today’s “apps” are now single page, complex, asynchronous mazes that require a more robust infrastructure and a stack of technologies which place PHP as just one little pieces of the today’s web development puzzle.

So the question arises: is it a good option to stick with PHP or is it time to move on? Well, I chose the later path and for that I start thinking of javascript. Why? Because after using php through Laravel, Symfony, Drupal for, javascript, jQuery, angular, sass, bower and composer I realized there was a (good) diversity of concepts I have to have in mind every time I need to start a new project.

I kind of got tired of having a web of technologies in some cases patched through plugins or libraries to make them work together. With that in mind I gave a chance to Node.js. Why? Because it unifies the language from server to client. I only have to “speak” javascript everywhere, not just at the browser but also at server level. I won’t lie to you, I was kind of reluctant to work with javascript on the server side, it was (and it is) a pain because, mostly, my brain works on PHP mode. The good part is that I already “know” javascript, the bad part is I have not realized I didn’t know javascript that much.

After a couple of weeks working on a very simple site with node I can say that I start to like it. There are many advantages of switching to a nowadays popular language like node.js.

  • Its community is large and active. All major and most important packages are actively maintained and updated.
  • More likely than not, there is already a built package that solves the problem you have. There are plenty of packages available from npmjs.com
  • Adding packages to a project is simple. Any package can be installed and added to the project by using npm.
  • Documentation is great. Packages are, in general, well documented and there are plenty of tutorials and examples of usage of the popular ones.
  • The whole thing is in the project. You don’t need a web server to run the site, the entire project is an independent software piece running on node. Obviously I’m not considering external services like a db.

I know some of these points are covered too with composer for instance or Drupal modules. However the interesting parts come when you need, for instance, to start using websockets, react or anything worth to integrate from client side.  Node handles this integration in a more native way and does not require external packages like zmq or so to run some library in order to support websockets. Even with Laravel if you want to use Elixir you need node and gulp.

But not all are roses. I worked many years with Symfony and Laravel and when I started a project in node the first thing I look for was a “framework”. Express was the logical option because its popularity, documentation and community. It is a modular framework which means it provides core functionality and everything out of normal has to be added. This approach grants some flexibility to the point that I couldn’t find a convention at its project structure. In other words, there is no standard approach when starting an express project from scratch(unless you use express-generator). There are boilerplates and even packages that builds a folder structure to organize your files however there is no convention for that so if you are moving from any PHP framework you probably miss that. Don’t get me wrong, in expressjs.com there is a reference to express-generator but you need to install it globally. This step is prior to install express itself but it is described in a different section of Getting Started

With this in mind, I basically face the issue of looking for packages that mimic the functionality of what I was used to work with. I came up with a list of them which I found very useful and of course, they are just the ones I found more suitable from my usability perspective.

I started a project that expose some data through a very simple api and consumes it from a frontend built in react. So I set a few list of requirements in order to provide the api features:

After installing Express, I had to add all these packages to be able to provide the requirements listed above. Sounds like a bit too much if with Laravel you only need to install Laravel, right? Well, yes, that’s true. From the standing point of just building an API from a model, I think Laravel o Drupal (with the right model) will speed up the development. However after you get familiar with the packages it is not hard at all to work with them.

The good part? I didn’t struggle with nginx/apache or php configuration. Everything runs from the project folder and http server can be configure from within the project. I didn’t have to worry about anything but to Mysql is running.

I’m pretty sure there are already projects providing features like Admin pages built on top of express but as I am a newbie at this, I want to start from basis and then move to easier implementations.

In my next post I will tell you how I used the packages I linked above. This was just to tell my short experience in what I hope may be a good start on the web development using node.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.