Weekends are fun for most people. But for me, it’s too boring. Staying alone and cooking your own food can be really boring job. So, every weekend I need to plan ahead what I would do make it a bit more interesting.
Being a PHP developer for years, stagnation really got into me. WordPress’ recent news about moving to Node and React was a wake-up call. So shaking off all the inertia, I loaded Node into an Ubuntu 14.04 VM and started building stuff.
I read on Quora (can’t find the link now) that, to properly learn a web technology, you need to build a simple app that has the following features:
2. Connecting to a database
3. Fetching data into a page
4. Image/file upload facility
5. Option to download data
Now, that is a very simple idea but it covers almost all the aspects of a web technology. Design should not be a concern because the important thing is to learn the technology, not to build stunning websites. Besides, the Internet is full of sites that give you snippets of Bootstrap that are ready to be plugged in for almost any use-case.
Apart from architecture, the whole idea of how to write web-pages is different. Since PHP can be easily mixed with HTML (but should not be), it’s pretty easy to learn and it is one of the reasons why people pick it up quickly. Being an old language, PHP has evolved over the years. Symfony, Laravel and many other frameworks have effectively separated HTML from business logic.
In node.js, you have to write logic separate from your HTML which is what it should be. The idea of routing is very important: only defined routes will output results, 404 for all others.
So what are the other differences?
For a PHP developer, a web page can only be accessible if it is placed under the document root which is generally /var/www or /srv/http (C:/*amp*/www for Windows users). But for Node.js, the idea of document root doesn’t exist, per se. You can run any application from any directory by just running a command and the application starts to ‘listen’ to a specific port. What that means is that if you run node app.js from ~/home/myapp, the app starts to listen to a specific port on the machine. If the port is set to 8080, you can access the app from http://localhost:8080. So, ~/home/myapp becomes the document-root for this specific app. .htaccess files are also non-existent. If you don’t define a route (i.e. a path to access a web-page), the path will give you a 404. So, vulnerabilities like open-directories are no longer a problem with Node.
While writing PHP code, we use include to access other files that provide functions or variables contained in included files. In case of Node, you have to specifically declare which functions or variables you want to export inside the included file. Files can be included by using the require function. Functions or variables declared otherwise cannot be used in other files. From a PHP dev’s mindset, each file is like a class: if you define public methods or variables, they can be used by other files.
Node’s dependency management is aesthetically beautiful. You just write a package.json file and run npm install to install all dependencies included in package.json. The dependencies will be inside node_modules directory. PHP does have such kind of dependency management if you use Composer. But I find most PHP devs avoiding to use Composer, for reasons beyond me.
In PHP, we generally write for one connection to the database using mysqli or other such libraries. Ideas like connection pooling do not exist in PHP. Whereas in Node, connection pooling can be used to effectively leverage MySQL’s features. This is provided by the mysql package. (I used mysql for my test project, not aware of such features available in other databases.)
These are some of the few differences I found while learning. This post is definitely not a comparison to find out which one is better. Being a PHP developer for more than 7 years, I absolutely love it and am more comfortable with it. But after learning the basics of Node, I found what I am going to use for my next pet project!
P.S.: Here is the project I worked on. Please feel free to fork and make changes!