Skip to content

Instantly share code, notes, and snippets.

@victorbruce
Last active October 9, 2019 16:19
Show Gist options
  • Save victorbruce/2a6c95eff1bdeaa0ac81af0f94857610 to your computer and use it in GitHub Desktop.
Save victorbruce/2a6c95eff1bdeaa0ac81af0f94857610 to your computer and use it in GitHub Desktop.

What is Nodejs

NodeJs is an open source runtime environment. Runtime simply means something that is executable in it's own environment. Nodejs was built on google's chrome v8 javascript engine.

Node is not a convention based platform where everything is thought through such as the database, sessions, authentication, etc. For example PHP, Ruby on Rails etc. NodeJs has no idea or does not know which database you are going to hook on to.

Node is not recommmended for high CPU intensive operations like reading the file system asynchronously, or machine learning or large arithemetic. Node is good when it comes to concurrency.

In summary, node is not a single bullet for everything.

What can I create with NodeJs

First of all, javascript can do pretty much anything other languages can do from tooling, building rest api, real time socket etc.

  • Great for tooling
  • Great for creating APIs (REST, Realtime, Sockets) because of it's high concurrency. It can handle a lot of request at the same time.
  • Building CDNs

In summary, node can do pretty much anything because node is on everything now.

Installing NodeJs

The advantage of NVM over node is that it is recommended and allows you to run many versions of node whenever. Moreover, nodejs causes permission errors

Running Nodejs code

There are two ways of evaluating your javascript code in node

Interactive REPL: This is mainly for playing around. It stands for READ, EVALUATE, PRINT, LOOP. It is an interactive environment for writing code and it is use for testing things out like the console in the browser. Either than that, it is not used for anything in terms of building full fledge applications. So the REPL is great for trying things out. Think of the console in the browser. To use REPL just tpe the node command with no args and begin

CLI Executable: Used for everything else. It is used to execute your node apps but now you pass the path to the file you want to run.

Globals in Nodejs

Node provides you with helpful globals such as:

process: has information about the environment the program is running in such as the path, OS version, environment variable etc. require: function to find and use modules in the curernt module.

__dirname: the current directory path.

module: is the sibling of require where modules are made consumable. Simply put, it is a way of exporting a module so that other modules can require them.

global: like window, it is the "global object". Almost never use this

__filename: gives you the current filename

What are modules?

Modules are basically encapsulated code. Nodejs uses commonJs for it's module system. Some of the other module systems out there are:

-ESM(ecmascript modules) -AMD(it's outdated)

Creating Node Modules

Creating modules in node in just regular code that needs to be exported.

**

NPX

First of all, npx comes with node. Npx allows you to run locally installed modules on the fly (eg. jest) rather than typing out the exact location of the module let's say >> node modules/jest

Also, incase the module is not installed, npx installs the package before running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment