Note: This post was originally written for Twitter
^ You can find the original here
1. Initialize the project
Follow the prompts from this command
touch index.js
npm init
2. The basics
Here is the basic layout of the server
// 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
// 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
// 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()
// 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
// 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.
Top comments (1)
I think you could add
https
it's only few lines of code and you can easily run both at the same time. you can also usemkcert
to easily generate certificates for local environments.you can just do
https.createServer()