Introduction to Node.js: Building a Simple Web Server

 Introduction to Node.js: Building a Simple Web Server


Node.js is a popular server-side JavaScript runtime that allows developers to build fast and scalable web applications. In this tutorial, we will introduce Node.js and build a simple web server using the built-in HTTP module.


Step 1: Installing Node.js


Before we start, make sure you have Node.js installed on your computer. You can download and install it from the official Node.js website.


Step 2: Creating the Server


Next, we will create a JavaScript file and add the following code to create a simple web server:


javascript


const http = require('http');


const hostname = '127.0.0.1';

const port = 3000;


const server = http.createServer((req, res) => {

  res.statusCode = 200;

  res.setHeader('Content-Type', 'text/plain');

  res.end('Hello World\n');

});


server.listen(port, hostname, () => {

  console.log(`Server running at http://${hostname}:${port}/`);

});

This code creates a web server that listens on port 3000 and responds with a plain text message "Hello World".


Step 3: Running the Server


Save the file with a .js extension, and run it using the node command:


Copy code

node server.js

You should see a message in the console that says "Server running at http://127.0.0.1:300


Step 4: Testing the Server


Open a web browser and navigate to http://127.0.0.1:3000/. You should see the "Hello World" message displayed on the page.


Step 5: Adding HTML and CSS to the Server


Next, we will modify the server to serve HTML and CSS files. Create two files, index.html and style.css, and add the following code to each file:


index.html


php


<!DOCTYPE html>

<html>

  <head>

    <title>Node.js Web Server</title>

    <link rel="stylesheet" type="text/css" href="style.css">

  </head>

  <body>

    <h1>Welcome to Node.js Web Server!</h1>

    <p>This is a simple web server built with Node.js.</p>

  </body>

</html>

style.css


css


body {

  background-color: #333;

  color: #fff;

  font-family: Arial, sans-serif;

}


h1 {

  font-size: 36px;

  margin-top: 50px;

}


p {

  font-size: 18px;

  margin-top: 20px;

}

Then, modify the server code to serve the index.html and style.css files:


javascript


const http = require('http');

const fs = require('fs');


const hostname = '127.0.0.1';

const port = 3000;


const server = http.createServer((req, res) => {

  if (req.url === '/') {

    fs.readFile('index.html', (err, data) => {

      res.writeHead(200, {'Content-Type': 'text/html'});

      res.write(data);

      res.end();

    });

  } else if (req.url === '/style.css') {

    fs.readFile('style.css', (err, data) => {

      res.writeHead(200, {'Content-Type': 'text/css'});

      res.write(data);

      res.end();

    });

  } else {

    res.statusCode = 404;

    res.end('404 Not Found');

  }

});


server.listen(port, hostname, () => {

  console.log(`Server running at http://${hostname}:${port}/`);

});

Now, when you navigate to http://127.0.0.1:3000/, you should see the index.html file served with the styles from style.css.


And that's it! With just a few lines of code, we have built a simple web server using Node.js that serves HTML and CSS files.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...