> For the complete documentation index, see [llms.txt](https://chloe-codes1.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chloe-codes1.gitbook.io/til/node.js/node.js-101/03_first_application_with_node.js.md).

# First Application with Node.js

\ <br>

### Creating a Node.js Application

<br>

#### Step 1: Import the required modules

* Use the `require` command to load modules needed for the application

  ```javascript
  var http = require("http");
  ```

  * Loads the HTTP module and stores the returned HTTP instance in the http variable

\ <br>

#### Step 2: Create the Server

* Execute the `http.createServer()` method using the http instance created in Step 1
* Use the `listen()` method to bind with **port 8081**
* Pass a function with request and response as parameters to http.createServer()

<br>

> main.js

```javascript

var http = require("http");

http.createServer(function (request, response) {
    /*
        Send HTTP Header
        HTTP Status: 200 : OK
        Content Type: text/plain
    */
   response.writeHead(200, {'Content-Type': 'text/plain'});

   /*
       Set Response Body to "Hello World"
   */
   response.end("Hello World\n");
}).listen(8081);

console.log("Server running at http://127.0.0.1:8081");
```

* Creates a web server on port 8081 that always returns "Hello World"

\ <br>

#### Step 3: Test the Server

<br>

> Run the server

```bash
node main.js
```

<br>

> If the server starts successfully, the following text is output

```bash
Server running at http://127.0.0.1:8081/
```

<br>

> Open <http://127.0.0.1:8081/> in a browser to see the following result

![image-20200321203909879](/files/-M6zqDxULBvhAaJtDlHA)

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/node.js/node.js-101/03_first_application_with_node.js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
