Horje
How to create a basic server using Express?

Express is a web application framework for NodeJS, simplifying the process of building robust and scalable web applications. It offers a simple design, making it easy to create APIs and handle HTTP requests and also support the middleware. Express is a popular choice for creating web servers and APIs in JavaScript.

Steps to Create a basic Server Using ExpressJS:

Step 1: Install the express package in your application using the following the command.

npm install express

Step 2: Create a server in a file named server.js.

Javascript

// server.js
const express = require('express');
const app = express();
const PORT = 3000;
 
// define the route
app.get('/', (req, res) => {
    res.send(`<h1 style="color: green;">Hello Gfg!</h1>`);
});
 
app.listen(PORT, () => {
    console.log(`Server is listening at http://localhost:${PORT}`);
});

Start your server using the following command:

node server.js

Output:

gfg3

Output




Reffered: https://www.geeksforgeeks.org


Node.js

Related
What is routing in Express? What is routing in Express?
What is Node? What is Node?
Node.js Tutorial | Learn NodeJS Node.js Tutorial | Learn NodeJS
Mongoose Tutorial Mongoose Tutorial
Top Node Development Trends in 2024 Top Node Development Trends in 2024

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14