DEV Community

Timothy_Odugbemi
Timothy_Odugbemi

Posted on

Getting started with NodeJs and Express (Backend development)

Node.js is a JavaScript runtime library that allows developers to run JavaScript on the server side (i.e., outside of a web browser). It allows you to build server-side applications using JavaScript, and is particularly useful for building real-time, high-concurrency web applications such as chat rooms and online games.

Express is a framework for building web applications and APIs with Node.js. It provides a set of features and functions that make it easy to build and customize server-side applications using Node.js.
Both go hand-in-hand.
To set-up node and express on your system, you will need the following:
A text editor: This can be any text editor that you are comfortable using, such as Sublime Text, Atom, or Visual Studio Code. (I use VScode, which I will also recommend greatly)

Node.js: This is the JavaScript runtime that allows you to run JavaScript on the server side. You can download the latest version of Node.js from the official website (https://nodejs.org/en/) and install it on your system.

npm (Node Package Manager): This is a package manager for Node.js that allows you to install and manage Node.js packages (libraries and tools). npm is included with Node.js, so you don't need to install it separately.

Once you have these tools installed, follow these steps:

Create a new directory for your project.

Then open a terminal or command prompt window, navigate to the project directory, and run the following command to create a package.json file:

npm init -y

Enter fullscreen mode Exit fullscreen mode

Then, you can install express with:

npm install express

Enter fullscreen mode Exit fullscreen mode

After that, create an entry point for your application by creating a file named index.js in the project directory.

Then open index.js in your text editor and import the Express module by adding the following line at the top of the file:

const express = require('express');

Enter fullscreen mode Exit fullscreen mode

Then, create an instance of an Express app by adding the following line:

const app = express();

Enter fullscreen mode Exit fullscreen mode

This should fall directly under the former.

Routes and middleware functions are needed in Express. Here is an example:
The following route will send a "Hello World!" message when the root URL is accessed:

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

Enter fullscreen mode Exit fullscreen mode

In order to be able to start the server, add the following line at the bottom of the file:

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

Enter fullscreen mode Exit fullscreen mode

That's it! You should now have a basic Node.js and Express server set up on your system. You can start the server by running the following command in the terminal:

node index.js

Enter fullscreen mode Exit fullscreen mode

That's just the fair introduction to Nodejs and Express. If you have any questions, please feel free to ask in the comment section below. Thanks

Top comments (0)