This week we learned about Node.js, which is a JavaScript runtime environment. We also delved into the world of npm or the Node Package Manager, which hosts the largest public registry of packages in the world. Initializing a Node project and installing a Node package from the command line is super simple:
npm init
npm i <package-name> --save
The first line initiates an interactive Node package set-up process. This creates a file called package.json, which houses information about your newly created Node package, including:
- name: Name of package
- version: Version of package
- description: Describes your package
- homepage: URL to project homepage
- bugs: URL to project’s issue tracker (If using GitHub, this will be your /issues path)
- license: ISC by default
- author: Name of developer
- main: Primary entry point to the program
- directories: A way of specifying the structure of the package
- repository: URL for the package’s repo (detected automatically if it is on GitHub)
- scripts: Dictionary containing script commands run at various times
- dependencies: Names and versions of packages that your package depends upon
The second line installs one or more packages in the node_modules path and updates the dependencies field in package.json.
One of the packages we worked with was Express.js. Express is a Node.js web framework that exposes a variety of middleware functions that allows a developer to parse incoming HTTP request bodies that contain JSON payloads.
To create an Express application, you first have to import the Express module and then create an instance of an Express application:
var express = require('express'); // import Express module
var app = express(); // Initialize Express app