Introducing Node.js

Welcome! Let's embark on a journey into server-side programming, starting with Node.js.

Before we pound the pavement, let's briefly define two core terms of web communication: server and client. You can think of the server as a chef - it prepares (serves) data or information. The client, on the other hand, is like a restaurant guest - it requests and receives this information and renders it on the web page. Together, they carry out web interactions.

Now, back to Node.js. So, what exactly is Node.js? It's a JavaScript runtime built on Chrome's V8 JavaScript engine, which allows you to run JavaScript on your computer. In other words, it enables you to use JavaScript outside of a browser! With Node.js, you can interact with your computer's file system much as you would with other server-side languages such as PHP or Python.

Dancing with Express.js

Next, let's explore Express.js. It's a minimal and flexible Node.js web application framework that provides the essential features needed to build web servers and APIs.

Express.js is intentionally lightweight - out of the box, it gives you just the basics for handling routes, requests, and responses. This minimalist approach means you can keep your application simple or extend it with additional packages as your needs grow.

To use Express.js in your application, require the express module in a Node.js file as shown below:

JavaScript
const express = require('express');
Building Our Simple Server

With Node.js and Express.js at the ready, let's proceed and create a simple server. Here's a piece of code that constructs a server that responds with "Hello World!" to all requests:

const express = require('express'); // importing Express.js
const app = express(); // Instantiating Express.js app server instance
const port = 5000; // The server will be listening on port 5000

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

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

This code starts by requiring the express module to use Express.js. We then create an instance of Express by calling the express() function, which returns our app. The app object possesses several methods, one of which is the get() method, which defines a route handler for HTTP GET requests. In this case, we are defining a route handler for the /api path that responds with the string "Hello World!" whenever it receives a GET request. Finally, we start our server on the defined port by calling the listen() method on our app.

When the server is started (it happens automatically when the task is opened in CodeSignal IDE), the message "Example app listening at http://localhost:5000" should appear. This signifies that your server is operational and listening on port 5000.

Note: A port is simply the location that our server is expecting to receive requests from. Similar to how you might expect to get files from a USB port on your computer or receive audio from a headphone port.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal