In this article, we’re going to learn how to create a server in Node.js using the popular Express framework. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Before we begin, make sure you have Node.js and npm (Node Package Manager) installed on your computer. You can download the latest version of Node.js from the official website (https://nodejs.org/en/download/).
The first step in creating a Node.js server is to create a new project directory. You can do this by running the following command in your terminal:
mkdir my-project
Next, navigate to your new project directory and initialize a new npm project by running the following command:
cd my-project npm init -y
This command will create a package.json
file in your project directory, which will keep track of any dependencies you install for your project.
Now that we have a new project set up, we can install Express by running the following command:
npm install express
Once Express is installed, create a new file in your project directory called index.js
. This file will contain the code for our server.
In index.js
, we’ll start by requiring the Express module and creating an instance of an Express application:
const express = require('express'); const app = express();
Next, we’ll create a route for the root of our application using the app.get()
method. This method takes two arguments: the path of the route and a callback function that will handle the incoming request:
app.get('/', (req, res) => { res.send('Hello, World!'); });
Finally, we’ll start our server on a specific port (in this case, port 3000) using the app.listen()
method:
app.listen(3000, () => { console.log('Server started on http://localhost:3000'); });
Now you can run your server locally by running the command:
node index.js
and by visiting http://localhost:3000 in your browser.
Full code:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(3000, () => { console.log('Server started on http://localhost:3000'); });
This is just a basic example of how to create a server in Node.js using Express. You can build on this example by adding more routes, handling different HTTP methods, and more.
You can also use the built-in http
module in Node.js to create a server, Here is a example
const http = require('http'); const server = http.createServer((req, res) => { res.end('Hello, World!'); }); server.listen(3000, () => { console.log('Server started on http://localhost:3000'); });
This code creates a server using the http.createServer()
method, and sets up a callback function that will handle incoming requests. The callback function takes two arguments: an req
object representing the incoming request, and an res
object representing the outgoing response. The callback function sends back the response “Hello, World!” to the client. The server will listen on port 3000 for incoming requests.
In this tutorial, we have learned how to create a Node.js project using npm, set up a server using Express, and handle HTTP requests. With this basic understanding, you can now start building more complex applications and take advantage of the vast ecosystem of Node.js packages available through npm.