Introduction

These are my notes as I go through The Complete Developer book by Martin Krause and a couple of other books. I am attempting to go through a few books to see if I can teach myself React, JavaScript, and MongoDB.

Definitions

Node.js – an open source runtime environment that runs JavaScript code outside of a web browser.

Check if Node.js is installed:

$ node -v
v22.7.0

Default package manager for Node.js is npm. To check it is installed:

$ npm -v
10.8.2

Creating a new project or initializing a new module. To start a new project, run npm init, which initializes a new module:

mkdir test-express
cd test-express
npm init

npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (test-express) 
version: (1.0.0) 
description: my first project
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/documentista/test-express/package.json:

{
  "name": "test-express",
  "version": "1.0.0",
  "description": "my first project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) 

The package.json file contains all the metadata about the project. At the very minimum, it must contain the project’s name and version. Running npm install <package> downloads and places a specific package in the node_modules folder, next to the package.json file, and adds it to the dependencies list in package.json.

We want to create a new Express.js-based server, so let’s install the express package:

npm install express@4.18.2

added 64 packages, and audited 65 packages in 2s

Now our package.json file looks like:

{
  "name": "test-express",
  "version": "1.0.0",
  "description": "my first project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  }
}

Now the optional field (dependencies) contains express. Dependencies are what are needed to run the project. These dependences are part of your application and when you install the project on a new machine, all the dependencies listed in package.json are installed and placed in the node_modules folder.

Now, let’s say you want to install a package called tox for automated testing. Instead of adding it to the dependencies, this package is only used during development and is not needed to run the application. You should run npm install --save-dev package to download the package and add it to the devDependencies list in the local package.json file:

npm install --save-dev tox@1.0.0
added 122 packages, and audited 187 packages in 3s

Now the package.json file looks like:

{
  "name": "test-express",
  "version": "1.0.0",
  "description": "my first project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "tox": "^1.0.0"
  }
}

The npm package manager automatically creates a package-lock.json file for each project. The package-lock.json file keeps track of the exact version of every package and it’s dependencies – the file is quite long. With all the modules version-locked, every time the npm install command is run, an exact clone of the original setup is created.

Other helpful commands:

npm audit – inspects the local package.json file for any vulnerabilities:

npm audit
# npm audit report

base64-url  <2.0.0
Severity: high
Out-of-bounds Read in base64-url - https://github.com/advisories/GHSA-j4mr-9xw3-c9jx
fix available via `npm audit fix`
node_modules/base64-url
  uid-safe  <=2.1.3
  Depends on vulnerable versions of base64-url
  node_modules/express-session/node_modules/uid-safe
    express-session  1.0.1 - 1.18.0
    Depends on vulnerable versions of cookie
    Depends on vulnerable versions of debug
    Depends on vulnerable versions of uid-safe
    node_modules/express-session

snip...

Use npm audit fix to fix any issues.

npm prune – Checks the local package-json file, compares it to the local node_modules folder, and removes all unnecessary packages. Can be used during development for general cleanup.

npm update – updates all install packages to their latest version.

npm uninstall <package> – used to remove a package and its dependencies from the local node_modules folder and package.json. Use it to remove packages you don’t need any more.

npm uninstall tox

removed 122 packages, and audited 66 packages in 463ms

found 0 vulnerabilities

npm install – install an existing project on a new machine. Create a new, empty folder and copy the package.json and package-lock.json files into it. Then run npm install inside the directory. Run npm install whenever you clone a repo or create a new project.

What is Express.js? Express.js is a fast, unopinionated web application framework for Node.js. It simplifies the process of building web applications and APIs by providing a robust set of features for web and mobile applications. Here are some key points:

  • Middleware: It uses middleware functions to handle requests, responses, and other operations. Middleware can be used for logging, authentication, error handling, etc.
  • Routing: Express allows you to define routes for your application easily. You can create different endpoints and associate them with specific request methods (GET, POST, etc.).
  • Flexible: It’s unopinionated, meaning it doesn’t impose strict structures or rules on how you should organize your application. This allows developers the freedom to structure their code as they see fit.
  • Integration: Express can be easily integrated with various databases (like MongoDB, MySQL), templating engines (like EJS, Pug), and other Node.js modules.
  • Performance: It’s designed for high performance and can handle a large number of simultaneous connections.

Creating a simple Hello World Express.js Server

Inside the test-express folder, create a index.js file:

const express = require('express');
const server = express();
const port = 3000;

server.get('/hello', function (req, res) {
    res.send('Hello World! 🖐️');
});

server.listen(port, function () {
    console.log('Listening on ' + port);
});
  1. Load express package into the file and instantiate the app, and define port to use.
  2. Create route for the server to respond to ever GET request sent to the /hello base URL with text.

Start the server by running node index.js in your terminal and then visit http://localhost:3000/hello in your browser:

node index.js
Listening on 3000

Conclusion

I learned what node.js is and what the different files are and what they do. Went through several npm commands and started an Express server.