nodejs
  1. nodejs-web-modules

Node.js Web Modules

Node.js has a number of built-in modules that make it easy to develop web applications. In this tutorial, we'll discuss some of the most common web modules, their syntax, examples, and how to use them.

HTTP Module

The HTTP module is a core Node.js module that provides an HTTP server and client for handling HTTP requests and responses. Here's an example of how to create a basic HTTP server using the HTTP module:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(8080, () => {
  console.log('Server running at http://localhost:8080/');
});

In the example above, we create an HTTP server by calling the createServer function from the http module, passing in a function that handles each incoming request. We set the status code to 200, the content type to "text/plain", and send the response body "Hello World". Lastly, we start the server by calling the listen function on the server object.

Express Module

The Express module is a popular Node.js module used for building web applications. It provides a number of features such as routing, middleware, and template engines. Here's an example of how to create a basic Express server:

const express = require('express')
const app = express()

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

app.listen(8080, () => {
  console.log('Server running at http://localhost:8080/');
})

In the example above, we create an Express application by calling the express function from the express module. We define a route for GET requests to the root URL that responds with "Hello World". Lastly, we start the server by calling the listen function on the app object.

URL Module

The URL module is a core Node.js module that provides utilities for URL resolution and parsing. Here's an example of how to parse a URL using the URL module:

const url = require('url');

const urlString = 'http://localhost:8080/foo/bar?param1=value1&param2=value2';
const parsedUrl = url.parse(urlString, true);

console.log(parsedUrl.protocol); // Output: http:
console.log(parsedUrl.host); // Output: localhost:8080
console.log(parsedUrl.pathname); // Output: /foo/bar
console.log(parsedUrl.query.param1); // Output: value1
console.log(parsedUrl.query.param2); // Output: value2

In the example above, we create a URL string and use the parse function from the url module to parse it into an object. We access the various components of the URL object by using its properties.

Summary

In this tutorial, we discussed some of the most common web modules in Node.js, including the HTTP module, Express module, and URL module. We covered the syntax, examples, and how to use these modules in your Node.js web applications.

It's important to remember that Node.js has a wide variety of other modules that can be used for web development, so it's worth exploring the Node.js documentation to discover what other modules you might find useful.

Published on: