Structure of my framework

Getting informations

First I’ve read some posts about the creation of a framework:

Let’s code a web server from scratch with NodeJS Streams! | Codementor
GitHub - antsmartian/lets-build-express: This repo contains chapters which explains how one can build a minimal express library.

I also looked at the GitHub repositories of some frameworks

GitHub - expressjs/express: Fast, unopinionated, minimalist web framework for node.
AdonisJS Framework

Based on all that reading I’m going to use the http module and not recreate an equivalent, it seems like too much work.

Since I’m coming from PHP I’ll try to do something similar to Symfony or Laravel. I don’t know yet if it’s a good idea but I’ll see.

Creating the structure

Like I said I will create some classes that could be found in a PHP framework, here is the structure that I’ll use for the start. It will probably change in the future.

  • Http
    • Message
    • Request
    • Response
  • Routing
    • Route
    • Router
    • Middleware
  • Server

Creation of the server class

Some examples

At the beginning I want to create a server class that will be the kernel of my framework. Everything will go through this.

So I’ve looked into some existing frameworks to see what they were doing. Here are some links:

express/express.js at master · expressjs/express
http-server/index.ts at develop · adonisjs/http-server

So for now my server class looks like this:

import * as http from "http"
import { Request } from '../Http/Request';
import { Router } from "../Routing/Router";

class Server {
    private server: http.Server;
    private router: Router;

    constructor() {
        this.router = new Router();
        this.requestListener = this.requestListener.bind(this);
        let self = this;
        this.server = http.createServer(this.requestListener);
    }
    requestListener = (req: http.IncomingMessage, res: http.ServerResponse) => {
        let request = new Request(req);
        this.router.handle(request, res);
    }

    listen(port: number) {
        this.server.listen(port)
    }
}

export { Server }   

Creating the base of all http classes

abstract class Message {
    protected body: string;
    constructor() {
        this.body = '';
    }

    getBody() {
        return this.body;
    }
}

export { Message }
import { IncomingMessage } from "http";
import { Message } from "../Message";

class Request extends Message {
    private request: IncomingMessage;

    constructor(req: IncomingMessage) {
        super();
        this.request = req;
    }
}

export { Request }
import { Message } from "../Message";

class Response extends Message {
    constructor(body: string = '') {
        super();
        this.body = body
    }
}
export { Response }

Routing bases

class Route {

}

export { Route }
import { Request } from "../../Http/Request";
import * as http from "http"
import { Response } from "../../Http/Response";

class Router {
    constructor() {
    }

    handle(request: Request, res: http.ServerResponse) {

        let response = new Response('My response');

        this.executeResponse(res, response);
    }
    private executeResponse(res: http.ServerResponse, response: Response) {
        res.writeHead(200);
        res.end(response.getBody());
    }
}

export { Router }

Starting the app

I can start the server with a TypeScript file like this, if I go to http://localhost:3000 I can see ‘My response’

import { Server } from '../src/Server';

let app = new Server();

app.listen(3000);

Conclusion

I’ve now created the base and structure for my framework. I’m sure there will be a lot of changes, but I think it’s a good start.

Github