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 repository 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 equivent, it seems too much.

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 class 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 exemples

For the beginin i want to create a server class that will be the kernel of my framework. Everything will go throught this.

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

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

So for now my server class look 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 stard.

Github