node.js - ilya-khadykin/notes-outdated GitHub Wiki

node.js

node.js is a server side implementation of Google Chrome js engine that allows you to run it on your server. It's event driven and asynchronous which makes it perfect for writing network and I/O intense applications.

Best practices

node.js differs a lot from conventional languages and frameworks, it doesn't require an http server to run (apache or nginx), instead you create your own http server inside your app and have full control of it.

Express is a recommended option for implementing http server with node, because it's lightweight and supports a lot of cool features like routing and templating engines. But there are other options (http module for example)

npm - node.js Packages

NPM (Node Package Manager) which is installed with node.js allows us to install Open Source JavaScript libraries developed by community

Initializing package.json

npm init

Installing an NPM Module

As a dev dependency:

npm install lite-server --save-dev

Configuring start script

Edit package.json and add required changes for your starter script under scripts declaration, for example:

  "scripts": {
    "start": "npm run lite",
    "test": "echo \"Error: no test specified\" && exit 1",
    "lite": "lite-server"
  },

Then you can invoke this command in a root directory of your project:

npm start

How node.js is scanning for mudules when require function is called

There are three types of modules:

  • file-based modules (CommonJS)
  • core modules (included in node.js by default)
  • node_modules

The order looks like this:

  1. file-name modules are passed in require function with prefixes \ or .\ or ..\
  2. if there is no prefix require('bar) node.js tries to find a module with name bar in core modules
  3. and only if it fails, as the last step, it looks for node_module called bar

Example

If a file /home/ryo/project/foo.js has a require call require('bar'), Node.js scans the file system for node_modules in the following order. The first bar.js that is found is returned.

  • /home/ryo/project/node_modules/bar.js
  • /home/ryo/node_modules/bar.js
  • /home/node_modules/bar.js -/node_modules/bar.js

In other words, Node.js looks for node_modules/bar.js in the current folder followed by every parent folder until it reaches the root of the file system tree for the current file or until a bar.js is found.

node_module looks exactly like file-name module, the only difference between file-based modules and node_modules is the way in which the file system is scanned to load the JavaScript file. All other behavior is the same.

Directory-Based Modules

It's a common practice to create directory-based modules using index.js inside the directory to include its content to the module.

npm configuration

Check the current settings:

npm config get registry
npm config get http-proxy
npm config get https-proxy

Set proxy:

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080