Building a Server with Node.js and Express.js

We will explain how to build a basic server using Node.js and Express.js. Based on this, you can add more complex features and develop real web applications by integrating with databases. Happy coding!

What is Node.js?

Node.js is a JavaScript runtime environment built on the Chrome V8 JavaScript engine. It allows JavaScript, which was previously only executed in the browser environment, to be executed on the server side, enabling the construction of server applications. Node.js operates on an asynchronous event-based model, providing efficiency in input/output operations and offering advantages in scalability and performance.

What is Express.js?

Express.js is a fast and flexible web application framework for Node.js. It provides a more abstracted API than Node's core functionalities, making it easier to develop web applications. With features such as routing, middleware, and session management, Express.js supports developers in building web applications efficiently.


Initial Setup for Building the Server

  • Name
    Project Initial Setup and Express.js Installation
    Type
    Description

    First, create a project folder and navigate to that folder. Then, install Express.js using npm.

Project Initial Setup and Express.js Installation

mkdir (folder name)
cd (folder name)
npm init -y
npm install express

Creating the index.js File

  • Name
    Project Initial Setup and Express.js Installation
    Type
    Description

    Create an index.js file inside the project folder and write the basic structure as follows.

Cautions

  • Name
    Project Initial Setup and Express.js Installation
    Type
    Description

    Use require instead of import

    The reason for using require is that Node.js follows the CommonJS module system by default. The CommonJS module system is the standard way used in Node.js to define and import modules, which was adopted by Node.js developers in the era when ECMAScript 5 or earlier versions of JavaScript did not support modules.

  • Name
    Extra
    Type
    Description

    The reason for using import in writing Frontend code with Node.js is that modern browsers and the latest Frontend development tools support the ES6 (ES2015) module system. Therefore, in Frontend development, you can define and import modules using import and export.

index.js

// Get the Express.js module
const express = require('express');
const app = express();
const port = 3000;

// Handler for requests to the root path
app.get('/', (req, res) => {
res.send('Hello, World!');
});

// Server execution
app.listen(port, () => {
  console.log(`The server is running at http://localhost:${port}.`);
});


Running the Server

  • Name
    Running the Server
    Type
    Description

    Use the command to run the Node.js server.

  • Name
    Browser Check
    Type
    Description

    When the server is successfully running, open your browser and access http://localhost:3000 to see the "Hello, World!" message.

Running the Server

node index.js