Start and Run Server in Node.js
×


Start and Run Server in Node.js

114

One of the most common use cases of Node.js is to build and run web servers. Node makes this task simple and efficient using its built-in http module. In this blog, we’ll walk through the steps to create a basic server and run it on your local machine using Node.js.

What You Need

Before getting started, ensure you have the following setup on your system:

  • Node.js installed (visit nodejs.org if not)
  • A code editor like VS Code
  • Basic knowledge of JavaScript

Step 1: Create a New Project Folder

Start by making a dedicated folder for your server project:

mkdir node-server-demo
cd node-server-demo

Step 2: Create a JavaScript File

Inside the project folder, create a file named server.js. This will contain the code for your server.

Step 3: Write Server Code

Open server.js in your editor and add the following code:

const http = require('http');

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to my Node.js server!');
});

const PORT = 3000;

server.listen(PORT, () => {
    console.log(`Server is running at http://localhost:${PORT}`);
});

This code creates a basic server that listens on port 3000 and responds with a plain text message.

Step 4: Run the Server

To run your Node.js server, open a terminal in the project directory and type:

node server.js

You’ll see the message:

Server is running at http://localhost:3000

Now, open your browser and go to http://localhost:3000. You should see the message: “Welcome to my Node.js server!”

Customizing the Response

You can modify the response based on the request URL. Here's an example:

const server = http.createServer((req, res) => {
    if (req.url === '/') {
        res.end('Home Page');
    } else if (req.url === '/about') {
        res.end('About Page');
    } else {
        res.statusCode = 404;
        res.end('Page Not Found');
    }
});

This allows you to serve different content based on the requested route.

Conclusion

Creating and running a server in Node.js is simple and powerful. With just a few lines of code, you can serve responses, handle routing, and begin developing full-scale backend applications. From here, you can explore more advanced setups using frameworks like Express.js, or add database support to handle dynamic content.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat