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:
// 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