How to Create a Server using Express JS #javascript #nodejs #expressjs #mernstack #nextjs14 #server #expressjs

Posted by

<!DOCTYPE html>

Creating a Server in Express JS

Creating a Server in Express JS

Express is a popular web framework for building web applications in Node.js. One of the key features of Express is its ability to easily create a server to handle HTTP requests.

Setting up Express JS

To create a server in Express, you first need to install the Express framework. You can do this by running the following command in your terminal:

npm install express

Creating a Server

Once you have Express installed, you can create a server by writing the following code in a file named server.js:

const express = require('express');
const app = express();
const port = 3000;

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

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

This code sets up a basic Express server that listens on port 3000 and responds with “Hello, World!” when a GET request is made to the root route (‘/’).

Running the Server

To start the server, you can run the following command in your terminal:

node server.js

After running this command, you should see a message in the console indicating that the server is running. You can then navigate to http://localhost:3000 in your browser to see the “Hello, World!” message.

Conclusion

In this article, we covered how to create a basic server in Express JS. Express provides a simple and efficient way to handle HTTP requests and build web applications in Node.js. By following the steps outlined above, you can quickly set up and run a server using Express.