Read 06: Node, Express, and APIs - Ahmad-Sawalqeh/amman-301d1 GitHub Wiki
Node:
What Is Node.js?
Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
How Do I Install Node.js?
Installing a Package Locally, open a terminal in that directory. Next type: npm init -y
This will create and auto-populate a package.json file in the same folder.
Next, use npm to install the lodash package and save it as a project dependency: npm install lodash --save
Create a file named test.js and add the following:
const _ = require('lodash');
const arr = [0, 1, false, 2, '', 3];
console.log(_.compact(arr));
And finally, run the script using node test.js. You should see [ 1, 2, 3 ] output to the terminal.
Working With the package.json File:
If you look at the contents of the test directory, you’ll notice a folder entitled node_modules. This is where npm has saved lodash and any libraries that lodash depends on. The node_modules folder shouldn’t be checked in to version control, and can, in fact, be re-created at any time by running npm install from within the project’s root.
If you open the package.json file, you’ll see lodash listed under the dependencies field. By specifying your project’s dependencies in this way, you allow any developer anywhere to clone your project and use npm to install whatever packages it needs to run.
What Are the Advantages of Node.js?
Node speaks JSON. JSON is probably the most important data exchange format on the Web, and the lingua franca for interacting with object databases (such as MongoDB).