Node is a JavaScript runtime that allows building of server-side and client-side applications.
Node is shipped with a package manager, called npm
, Node package manager, similar to Cocoapod or Swift Package manager allows for the installation of third party dependencies.
A package in Node can be used to add the following:
- Libraries
- Tools
- Shared Components
- Your app
2. npmjs.com
An online egistry to search for node packages.
The package.json
file is created when the following command is ran in terminal:
npm init
The package.json
file contains the following:
- Metadata - your project's name, version, descrption, author, repository, license, etc
- Dependencies - the list of packages used by your project
- Scripts - commands to automate development tasks e.g
npm start
will run the application server
Example of a package.json
file
{
"name": "demo-web-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Alex Paul",
"license": "ISC"
}
To bypass initialization questions
npm init -y
Download and add package and dependencies to be included in production.
npm install <package-name>
Download and add package to dev dependencies that will only be used for development and not shipped with the app.
npm install --save-dev <package-name>
The readline
package allows for reading in user input from the command line.
npm install readline
const readline = require("readline");
const r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r1.question('Please enter your name?', (name) => {
console.log(`Hello, ${answer}, welcome to Node development.`);
r1.close(); // need to close the readline connection to return to the application's regular execution
});
Prettier is used to format JavaScript code.
Install prettier
npm install --save-dev prettier
Edit the package.json
scripts property to allow for quick running of format
"scripts": {
"format": "prettier --write *.js"
}
Usage
npm run format
Nodemon is used to automatically watch for changed files and re-run the node server.
Install nodemon
npm install -g nodemon // using the -g flag enables the package to be available globally throughout applications
Usage, now instead of using node <script name>
to run a script we use:
nodemon <script name>
To restart a running application
rs
express
is a Node package that allows us to create web servers to host APIs that allow for http request, reponses and supports HTTP methods e.g GET and POST.
Install express
npm install express
An example of web server JavaScript code that will listen on a specific port and render "Hello, world" if the API address is visited.
Save the following code in a file called index.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (request, response) => {
response.send(`Hello World!`);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
To run the express
web server
node index.js
Edit the package.json
to include a run script
"scripts": {
"start": "node index.js"
}
Using the run script
npm start
dotenv
allows for environment variables configuration, such as managing API keys and credentials.
Install dotenv
npm install dotenv
Edit the web server JavaScript file
require('dotenv').config();
const express = require('express');
const app = express();
const port = process.env.PORT;
console.log(port); // 5000
Create a .env
file
PORT=5000
Re-run the web server and all should still work as before, only this time the the port number is not exposed in the JavaScript code.
Don't forget to add your
env
file to the.gitignore