top of page
Search

Node.js & Express.js Notes

Updated: Apr 18, 2020


Install node from node.js website


On command line to run a .js file:

$ node index.js

To type javascript on console:

$ node
$ console.log("hello world");

Using node.js modules:


index.js

const fs = require("fs");
// download File System module from node

fs.copyFileSync("file1.txt","file2.txt");
// use fs method to copy files

NPM (Node Package Manager) to Import External Modules


Go to directory where index.js is and where you want to use npm

intro-to-node artur$ npm init

//select options of package

//package name: intro-to-node

//entry point: index.js


after package.json created

intro-to-node artur$ npm install <package name, example superheroes>

// a node-modules file is created automatically in folder

// package.json updated with dependencies showing new downloaded package


index.js

var superheroes = require("superheroes");

superheroes.random();

EXPRESS.js with Node.js


Hello world example

my-express-server artur$ touch server.js
my-express-server artur$ npm init

// package name: my-express-server

// entry point: server.js


after package.json created

$ npm install express

express added to dependencies of package.json file of modules

in node_modules folder you'll see express module


const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

or

const express = require('express');
const app = express();
//create a function called app
//best practice to use app

app.get("/"function(requestresponse){
 response.send("<h1>hello world</h1>");
})
/*first parameter: "/" is the location of the get request, so sent 
to root of the website requesting access to our express app server. Think of "/" as the root of a website like www.arturodevesa.com.
second parameter: when that get request happens, we can trigger a call 
back function with two parameters, request and response*/

app.listen(3000function(){
 console.log("Server started on port 3000");
})

Note: This app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.


The example above is actually a working server: Go ahead and click on the URL shown. You’ll get a response, with real-time logs on the page, and any changes you make will be reflected in real time. This is powered by RunKit, which provides an interactive JavaScript playground connected to a complete Node environment that runs in your web browser. Below are instructions for running the same app on your local machine.

RunKit is a third-party service not affiliated with the Express project.


Running Locally


First create a directory named myapp, change to it and run npm init. Then install express as a dependency, as per the installation guide.

In the myapp directory, create a file named app.js and copy in the code from the example above.


The req (request) and res (response) are the exact same objects that Node provides, so you can invoke req.pipe(), req.on('data', callback), and anything else you would do without Express involved.


Run the app with the following command:

$ node app.js

Then, load http://localhost:3000/ in a browser to see the output.



List of HTTP status codes


This is a list of Hypertext Transfer Protocol (HTTP) response status codes. Status codes are issued by a server in response to a client's request made to the server. It includes codes from IETF Request for Comments (RFCs), other specifications, and some additional codes used in some common applications of the HTTP. The first digit of the status code specifies one of five standard classes of responses. The message phrases shown are typical, but any human-readable alternative may be provided. Unless otherwise stated, the status code is part of the HTTP/1.1 standard (RFC 7231).[1]

The Internet Assigned Numbers Authority (IANA) maintains the official registry of HTTP status codes.[2]

All HTTP response status codes are separated into five classes or categories. The first digit of the status code defines the class of response, while the last two digits do not have any classifying or categorization role. There are five classes defined by the standard:

  • 1xx informational response – the request was received, continuing process

  • 2xx successful – the request was successfully received, understood, and accepted

  • 3xx redirection – further action needs to be taken in order to complete the request

  • 4xx client error – the request contains bad syntax or cannot be fulfilled

  • 5xx server error – the server failed to fulfil an apparently valid request




Recent Posts

See All

Generative AI report

Top GenAI companies: OpenAI Google Anthropic Meta Mistral Stability AI MidJourney Top GenAI Models GPT 4 Gemini 1.5 Llama2 Mistral Claude Stable Diffusion

bottom of page