Hope everyone is doing good, In this article we are going to be creating an Express.js app which literally involves some settings for the foundational structure for the web application to print out the basic expression like "hello world", there are even other concepts to work with high-end web applications or the applications that we have to work on to get them in production such as defining routes, configuring middleware, and managing all the dependencies. By the way in this article we are going to be writing the basic code to print the "hello world", the ExpressJs code we are going to write in this article will be the basic and simple way to print the expression "like "Hello world".
Prerequisites
Before writing the code we need to fulfill ours necessary things to start it, make sure you have Node.js and npm (Node Package Manager) installed on your system. If not, download and install them from the official website: https://nodejs.org/
Create a Project Directory
Create a new directory for your Express.js app and navigate to it using your terminal or command prompt:
mkdir my-express-app
cd my-express-app
Initialize a Node.js Project
Run the following command to create a package.json
file to track your project's dependencies:
npm init -y
Install Express.js
Install Express.js as a project dependency:
npm install express
Create an Express App
Create a JavaScript file (e.g., app.js
or index.js
) to define your Express application:
const express = require('express');
const app = express();
const port = 3000; // You can choose any available port
// Define a route
app.get('/', (req, res) => {
res.send('Hello, Express.js!');
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
In this example:
- We import the Express module.
- Create an instance of the Express application.
- Define a simple route that responds with "Hello, Express.js!" when you access the root URL ("/").
- Start the server to listen on port 3000 (you can choose a different port).
Start the Express Server
Run the following command to start your Express server:
node app.js
You should see the "Server is running on port 3000" message in the terminal, indicating that your server is running.
Access Your Express App
Open a web browser or use a tool like curl to access http://localhost:3000
(or the port you specified) to see the "Hello, Express.js!" message.
Your basic Express.js app is now up and running. You can expand it by adding more routes, middleware, templates, and functionality according to your project's requirements. Express.js provides a flexible framework for building web applications and APIs.