# 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](https://199941116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M6ivT9AfNVmiT1Q6B2U%2Fuploads%2Fgit-blob-da1ac6f13ee6c11f3db3f916ed407d4d9fdbade1%2Fimage-20200321203909879.png?alt=media)

<br>


---

# Agent Instructions: 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.
