A barebones HTTP/1.1 server built only with raw TCP sockets (net module), implemented my own routing system
No frameworks used.
$ git clone https://github.com/greatm3/HTTP-from-scratch
$ cd HTTP-from-scratch
# Install dependencies
npm install
# compiles the typescript to js, creates a dist/ directory
npm run build
import HTTPServer from "./dist/HttpServer.js";
const server = new HTTPServer()
server.makeServer((req, res) => {
res.setHeader("Content-Type", "text/html")
res.write("<h1>Hello</h1>")
})
server.listen(3971, () => {
console.log("Started at 127.0.0.1:3971")
})- open your browser and go to
http://127.0.0.1:3971
// HTTP server class inherits from EventEmitter class, so on recieving data, the instance emits a request message
server.on("request", (req, res) => {
// knock yourself out.
})- Basic HTTP request parsing
- GET method routing
- Response object with status/headers
- POST method with body parsing
- Query parsing
- This project is for learning purposes. Not intended for production use, see LICENSE.