Concepts in Node.js
- Node.js uses an event-driven, non-blocking I/O model. Functions like Querying Db, file system read/write operations are designed to be asynchronous non-blocking. Task completion will be signed through callbacks. Special care needs to be taken to avoid callback hell
Node.js is single threaded. You need to use process managers like pm2 to spin additional instance of your application to efficiently use the available cores on the server.
Node follows the Unix principle. One utility per file. Npm modules should be used like Lego blocks for building the software.
Validation logic, post records insertion logic are all written as middleware. It is Middleware not middlewares
Dependency management – npm & bower
They're all good package managers, really.
— isaacs 💙💜💖 (@izs) December 20, 2016
npm, a word play, is the dependency manager for Node.js. Dependencies are maintained in
package.json and are installed in
It used for installing front-end assets like HTML, CSS, JS. The dependency details are maintained in
bower.json and are installed in
bower_components/ folder. These days, more frontend framework are preferring npm over bower for managing dependencies.
Express is the most popular, widely used framework in the Node land. It is partly, because it is the “E” of the “MEAN” stack. Also, it is unopinionated. You can use routes to handle request – response, controller for db model interaction. It is easy to set up and run with it.
There are also other notable frameworks such as Walmart’s hapi.js, Paypal’s kraken.js. If you’re coming from Rails background, you can check sails.js. It tries to solve the same problem that Rails had solved for Ruby.
Tool set – IDEs, Linting, task runners
IDEs like WebStorm will have everything integrated into the environment out of the box.
Guides – indentation
You can run the Node.js application in any port you want, but it is highly desirable to have a public url address without port number. Since the default HTTP port 80 falls under 1024, you could require a sudo access to run the application on this port. This may lead to conflict with the web servers like Apache or Nginx that require port 80 to run. We can avoid this by the starting the application in ports > 1024 using process manager like pm2 and then fronting it with Nginx.