Make a simple HTTP Server with Node in 6 Steps
Tags: node, javascript
Published: 2020-03-05 23:56:47

**Note**: *This post was originally written for Twitter*
^ You can find the original [here](https://twitter.com/su_andrewk/status/1235717546827018241)

## 1. Initialize the project
Follow the prompts from this command
```bash
touch index.js

npm init
```

## 2. The basics
Here is the basic layout of the server
```javascript
// Import the HTTP module
const http = require('http')

// Make our HTTP server
const server = http.createServer((req, res) => {
    res.write('hello world')
    res.end()
})

// Have the server listen on port 9000
server.listen(9000)
```

## 3. Routing
Routing can be done by importing the builtin URL Module and parsing the request URL. Then, we compare the request URL to the URL for the route
```javascript
// Import the HTTP module
const http = require("http");
// Import the URL module
const url = require("url");

// Make our HTTP server
const server = http.createServer((req, res) => {
    // Parse the request url
    const reqUrl = url.parse(req.url).pathname
    if(reqUrl == "/") {
        res.write("you're boring")
        res.end()
    }
    else if(reqUrl == "/hello") {
        res.write("hello world")
        res.end()
    }
})
// Have the server listen on port 9000
server.listen(9000)
```

## 4. Methods
Similar to routes, we can check the method of the request and compare it the expected result
```javascript
// Import the HTTP module
const http = require("http");
// Import the URL module
const url = require("url");

// Make our HTTP server
const server = http.createServer((req, res) => {
    // Parse the request url
    const reqUrl = url.parse(req.url).pathname
    // Compare our request method
    if (req.method == "GET") {
        if (reqUrl == "/") {
            res.write("you're boring")
            res.end()
        }
    } else if (req.method == "POST") {
        if (reqUrl == "/hello") {
            res.write("hello world")
            res.end()
        }
    }
})
// Have the server listen on port 9000
server.listen(9000)
```

## 5. Headers
We can set headers on the response pretty easily using `res.setHeader()`

```javascript
// Import the HTTP module
const http = require("http");
// Import the URL module
const url = require("url");

// Make our HTTP server
const server = http.createServer((req, res) => {
    // Set our header
    res.setHeader("Access-Control-Allow-Origin", "*")
    // Parse the request url
    const reqUrl = url.parse(req.url).pathname
    // Compare our request method
    if (req.method == "GET") {
        if (reqUrl == "/") {
            res.write("you're boring")
            res.end()
        }
    } else if (req.method == "POST") {
        if (reqUrl == "/hello") {
            res.write("hello world")
            res.end()
        }
    }
})
// Have the server listen on port 9000
server.listen(9000)
```

## 6. Request Parameters
We can easily grab our URL based query strings with a little modification
```javascript
// Import the HTTP module
const http = require("http");
// Import the URL module
const url = require("url");

// Make our HTTP server
const server = http.createServer((req, res) => {
    // Set our header
    res.setHeader("Access-Control-Allow-Origin", "*")
    // Parse the request url
    const parsed = url.parse(req.url, true)
    // Get the path from the parsed URL
    const reqUrl = parsed.pathname
    // Compare our request method
    if (req.method == "GET") {
        if (reqUrl == "/") {
            // Send a JSON version of our URL query
            res.write("Hello, you sent\n" +  JSON.stringify(parsed.query))
            res.end()
        }
    } else if (req.method == "POST") {
        if (reqUrl == "/hello") {
            res.write("hello world")
            res.end()
        }
    }
})
// Have the server listen on port 9000
server.listen(9000)
```

## Wrapping things up
I hope you enjoyed the quick tutorial. I did most of the research for this while developing my own HTTP framework, called `onlinx`, built lightning fast and fully dependency-less. If you have any questions, feel free to comment them. If you are interested in `onlinx` please let me know. Thank you for reading and goodbye.