Getting Started with Node.js & Express

Node.js is a JavaScript runtime environment. The Node run-time environment includes everything you need to execute a program written in JavaScript. As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.

Category:

javascript

Getting Started with Node.js & Express

Harsh Vardhan

Tue Feb 09 2021・3 min read

Hey folks! Welcome to my article on getting started with Node.js and express and today we will be learning WTF is Node.js and Express.js. So, without any further do let's get started.

What is Node.js?

Node.js is a JavaScript runtime environment. The Node run-time environment includes everything you need to execute a program written in JavaScript. As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.

JavaScript now has the capability to do things that other scripting languages like Python can do.

Both your browser JavaScript and Node.js run on the V8 JavaScript runtime engine. This engine takes your JavaScript code and converts it into a faster machine code. Machine code is low-level code that the computer can run without needing to first interpret it.

What Can Node.js Do?

nodejs

  • Node.js can generate the dynamic page content
  • Node.js can create, open, read, write, delete, and close files on the server
  • Node.js can collect form data
  • Node.js can add, delete, modify data in your database

What is a Node.js File?

  • Node.js files contain tasks that will be executed on certain events
  • A typical event is someone trying to access a port on the server
  • Node.js files must be initiated on the server before having any effect
  • Node.js files have extension ".js"

Node.js Installation

Node.js can be installed in different ways. This post highlights the most common and convenient ones.

Official packages for all the major platforms are available at https://nodejs.org/en/download/. One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own.

On macOS, Homebrew is the de-facto standard, and - once installed - allows to install Node.js very easily, by running this command in the CLI:

brew install node

Other package managers for Linux and Windows are listed in https://nodejs.org/en/download/package-manager/

Let's create Hello World Node.js App

 

The code tells the computer to write "Lol Boi!" if anyone (e.g. a web browser) tries to access your computer on port 3000.

How can I do the same?

First of all install node.js on your machine. The tutorial mentioned above. Then, create a directory named myapp, change the directory to it, and run npm init. Then install express as a dependency

$ npm install express --save

In the myapp directory, create a file named server.js or whatever you like and copy in the code from the example above.

The req (request) and res (response) are the exact same objects that Node provides so that 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.

This was all about the basics of node.js and express framework and if you want to know more then do follow me on My Instagram. Comment down your queries and thanks for reading till the end.