Node.js, a JavaScript runtime built on Chrome's V8 engine, has revolutionized server-side programming by enabling developers to build highly scalable and efficient network applications. Express.js, a popular web application framework for Node.js, simplifies the process of creating web servers and APIs. Let's dive into an example of creating a simple web server using Node.js and Express.js.

Before we start, ensure you have Node.js and npm (Node Package Manager) installed on your system. You can verify the installation by running the following commands in your terminal:

Project Setup and Initialization
First, let's set up a new Node.js project and install the required dependencies.

1. Initialize a new Node.js project by running npm init -y. This will create a package.json file with default settings.
Installing Express.js

Next, install Express.js as a dependency by running npm install express. This command will download and install the latest version of Express into your project's node_modules folder.
Creating the Server
Now, let's create a new file named server.js and set up a basic Express.js server. Open your favorite code editor and write the following code:

```javascript const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); }); ```
The code above requires the Express module, creates a new Express application, and sets up a simple route that responds with 'Hello, World!' when visiting the homepage ('/'). It also listens on port 3000 and logs a message to the console once the server starts.
Running the Server
To start the server, run node server.js in your terminal. Now, if you open your browser and navigate to http://localhost:3000, you should see the message 'Hello, World!' - a sign that your Express.js server is up and running.

Adding More Routes
Express.js makes it easy to add more routes to your application. Let's add a new route that responds with the current date and time when visiting '/date'.



















```javascript app.get('/date', (req, res) => { const currentDate = new Date(); res.send(`The current date and time is ${currentDate.toLocaleString()}`); }); ```
Now, when you visit http://localhost:3000/date, you'll see the current date and time.
Using Middleware Functions
Express.js allows you to use middleware functions to handle common tasks like request logging, parsing request bodies, and more. For example, let's add a middleware function to log incoming requests:
```javascript app.use((req, res, next) => { console.log(`${req.method} ${req.url} - ${new Date().toISOString()}`); next(); }); ```
Now, every time a request is made to your server, it will be logged to the console.
Conclusion and Next Steps
By following this example, you now have a basic understanding of creating a web server using Node.js and Express.js. You've set up a project, installed dependencies, created routes, and even added middleware functions. As you continue exploring Express.js, you can delve into topics like handling HTTP methods (POST, PUT, DELETE), working with JSON, and building more complex APIs. Don't forget to check out the official Express.js documentation for more examples and features. Happy coding!