Node.js web-based application

Node.js web application contains different types of modules which is imported using require() directive and we have to create a server and write code for the read request and return response. Make a file web.js with the following code. 

Example:

javascript
// Require http module
let http = require("http");

// Create server
http.createServer(function (req, res) {

    // Send the HTTP header
    // HTTP Status: 200 : OK
    // Content Type: text/plain
    res.writeHead(200, { 'Content-Type': 'text/plain' });

    // Send the response body as "This is the example
    // of node.js web based application"
    res.end('This is the example of node.js web-based application \n');

    // Console will display the message
}).listen(5000,
    () => console.log('Server running at http://127.0.0.1:5000/'));

To run this file follow the steps as given below:

  • Search the node.js command prompt in the search bar and open the node.js command prompt.
  • Go to the folder using cd command in command prompt and write the following command node web.js
  • Now the server has started and go to the browser and open this url localhost:5000

You will see the response which you have sent back from web.js in the browser. If any changes are made in the web.js file then again run the command node web.js and refresh the tab in the browser.

Node.js Basics

Node.js is a powerful runtime environment that allows developers to build server-side applications using JavaScript. In this comprehensive guide, we’ll learn all the fundamental concepts of Node.js, its architecture, and examples.

Table of Content

  • What is Node.js?
  • Key features of Node.js
  • Setting Up Node.js
  • Core Modules
  • Datatypes in Node.js
  • Node.js console-based application
  • Node.js web-based application
  • Common Use Cases
  • Conclusion

Similar Reads

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows the creation of scalable Web servers without threading and networking tools using JavaScript and a collection of “modules” that handle various core functionalities. It can make console-based and web-based node.js applications....

Key features of Node.js

Non-blocking I/O: Node.js is asynchronous, enabling efficient handling of concurrent requests.Event-driven architecture: Developers can create event-driven applications using callbacks and event emitters.Extensive module ecosystem: npm (Node Package Manager) provides access to thousands of reusable packages....

Setting Up Node.js

Before coming into Node.js development, ensure you have Node.js and npm installed. Visit the official Node.js website to download the latest version. Verify the installation using:...

Core Modules

Node.js includes several core modules for essential functionality. Some commonly used ones are:...

Datatypes in Node.js

Node.js contains various types of data types similar to JavaScript....

Node.js console-based application

Make a file called console.js with the following code....

Node.js web-based application

Node.js web application contains different types of modules which is imported using require() directive and we have to create a server and write code for the read request and return response. Make a file web.js with the following code....

Common Use Cases

Building RESTful APIsReal-time applications using WebSocketsMicroservices architectureServerless functions with AWS Lambda...

Conclusion

Node.js is a versatile platform for building scalable and efficient applications. Dive deeper into its features and explore real-world use cases....