Backend – Basic Program to Make a ExpressJs Server
ExpressJs is a popular web application framework for Node.js that enables the creation of server-side applications. In this article, we will discuss a basic program to create a ExpressJs server.
Step 1: Install ExpressJs
Before creating a ExpressJs server, you need to install ExpressJs using npm. Open your terminal and run the following command:
npm install express
Step 2: Create a Server File
Create a new file called server.js
in your project directory. This file will contain the code for creating the ExpressJs server. Copy and paste the following code into server.js
:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
Step 3: Start the Server
To start the ExpressJs server, run the following command in your terminal:
node server.js
You should see the message ‘Server is running on port 3000’ in your terminal. You can now access your server by navigating to http://localhost:3000
in your web browser.
Conclusion
Congratulations! You have successfully created a basic ExpressJs server. This is just a simple example, and you can further customize and expand your server based on your project requirements. Happy coding!