Getting Started with Node
fundamental conceptions of Node.js
- Node is a runtime environment for executing JS code.
- Essentially, Node is a C++ program that embeds Chrome’s v8 engine, the fastest
JS engine in the world. - We use Node to build fast and scalable networking applications. It’s a perfect
choice for building RESTful services. - Node applications are single-threaded. That means a single thread is used to
serve all clients. - Node applications are asynchronous or non-blocking by default. That means
when the application involves I/O operations (eg accessing the file system or the
network), the thread doesn’t wait (or block) for the result of the operation. It is
released to serve other clients. - This architecture makes Node ideal for building I/O-intensive applications.
- You should avoid using Node for CPU-intensive applications, such as a video
encoding service. Because while executing these operations, other clients have
to wait for the single thread to finish its job and be ready to serve them. - In Node, we don’t have browser environment objects such as window or the
document object. Instead, we have other objects that are not available in
browsers, such as objects for working with the file system, network, operating
system, etc.
Node Core
- We don’t have the window object in Node.
- The global object in Node is “global”.
- Unlike browser applications, variables we define are not added to the “global”
object. - Every file in a Node application is a module. Node automatically wraps the code
in each file with an IIFE (Immediately-invoked Function Expression) to create
scope. So, variables and functions defined in one file are only scoped to that file
and not visible to other files unless explicitly exported. - To export a variable or function from a module, you need to add them to
module.exports:
module.exports.sayHello = sayHello;
- To load a module, use the require function. This function returns the
module.exports object exported from the target module:
const logger = require(‘./logger’);
- Node has a few built-in modules that enable us to work with the file system, path
objects, network, operating system, etc. - EventEmitter is one of the core classes in Node that allows us to raise (emit) and
handle events. Several built-in classes in Node derive from EventEmitter. - To create a class with the ability to raise events, we should extend EventEmitter:
class Logger extends EventEmitter { }
node package management
const _ = require("underscore")
// how node resolve this import
// Core module
// File or folder
// node_modules
the version of dependent modules on package.json
{
"name": "npm-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mongoose": "^5.3.13", //Major.Minor.Patch
"underscore": "^1.9.1"
}
}
semantic versioning(samver)
if there is a bug have been fixed and republished to the public, it will be 5.3.14 for mongoose.
if there are some new api been added, it will be 5.4.0 for mongoose.
However, with patch version of 0 is meaning that it is really unstable since it had never been found any bugs since it was published.if they find any bug and fixed it, then they will upgrade the patch version.
if they add a new feature that could potentially break existing the applications that depend upon this version of Mongoose, then they will increase the major version.
^: carrot character e.g. ~4.13.6 = 4.x
~: tilda character e.g. ~1.8.3 = 1.8.x
use npm list
to see all the modules that your project used and their dependencies. if you are only care about the modules your applications used, use npm list --depth=0
if you want to see the details of the module you are using, try this command to see: npm view ${module_name(e.g. mongoose)}
if you only want to see what dependencies the module used, try this command to see: npm view ${module_name} dependencies
{
async: '2.6.1',
bson: '~1.1.0',
kareem: '2.3.0',
'lodash.get': '4.4.2',
mongodb: '3.1.10',
'mongodb-core': '3.1.9',
'mongoose-legacy-pluralize': '1.0.2',
mpath: '0.5.1',
mquery: '3.2.0',
ms: '2.0.0',
'regexp-clone': '0.0.1',
'safe-buffer': '5.1.2',
sliced: '1.0.1'
}
if you want to install or upgrade to a specific version of a module, use this command: npm i mongoose@2.4.2
use npm outdated
to check if the versions you are using is needed to be updated or not.
e.g.
Package Current Wanted Latest Location
underscore 1.4.0 1.9.1 1.9.1 npm-demo
use npm update
to update your module with the wanted version it was shown on the list above.
If the major version of your current version and latest version is different and you wang to update this version, this command will help you: sudo npm i -g npm-check-updates
if you want to install some modules that are only using development dependencies and it should not go in the production environment, use this command to intall it npm i jshint --save-dev
. After intalled, it will be shown in the ‘devDependencies’.
To unintall a module: npm unintall mongoose
or npm un mongoose
Publish a Package
to publish a package on NPM, first you should create an account.
npm adduser
or if you have already created an account, use npm login
to login your account.
And then use npm publish
.
Don’t forget to make a unique package name.