1 / 8

Node.JS First Application

https://techvidvan.com/tutorials/nodejs-first-application/

ayushii12
Download Presentation

Node.JS First Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Node.JS Node.JS First First Application Application By Techvidvan

  2. Node.JS First Application Node.js provides a built-in HTTP module that allows developers to create web servers and handle HTTP requests and responses. In this article, we will walk through the process of creating a simple Node.js application using the HTTP module. By the end, you’ll have a basic understanding of how to set up a server, handle requests, and send responses.

  3. Prerequisites for First Application in NodeJS: Before we begin, make sure you have Node.js installed on your machine. You can download the latest version from the official Node.js website (https://nodejs.org) and follow the installation instructions for your operating system.

  4. Step 1 Set Up Your Project: 1. Create a new folder for your project and navigate to it using a command prompt or terminal. 2. Initialize a new Node.js project by running the following command: npm init -y This command creates a package.json file that tracks your project’s dependencies and configuration.

  5. Step 2 Write Your Application Code: Open ‘app.js’ in your preferred text editor or integrated development environment (IDE) and add the following code: const http = require('http'); const server = http.createServer((req, res) => { // Set the response headers res.writeHead(200, { 'Content-Type': 'text/plain' }); // Send a response to the client res.end('Hello, World!'); }); const port = 3000; server.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });

  6. Step 2 Write Your Application Code: In this code, we import the ‘http’ module and use its ‘createServer’ method to create an HTTP server. The server object listens for incoming requests and responds with the message “Hello, World!”. We set the response headers using ‘res.writeHead’ with a status code of ‘200’ and the content type as ‘text/plain’. Finally, we send the response back to the client using ‘res.end’. The server listens on port 3000 using the ‘listen’ method. The callback function is executed once the server starts, and it logs a message to the console indicating that the server is running.

  7. Step 3 Run Your Application: 1. Open a command prompt or terminal and navigate to your project folder. 2. Start your application by running the following command: node app.js 3. Open your web browser and visit http://localhost:3000. You should see the message “Hello, World!” displayed on the page.

  8. Conclusion In this article, we learned how to create a basic Node.js application using the HTTP module. We set up a server, handled incoming requests, and sent responses back to the client. The HTTP module provides a foundation for building more complex web applications in Node.js. Feel free to experiment and extend this application to explore more features and functionalities.

More Related