OOP In TypeScript + Express Server

Prasad Hegde
4 min readMar 23, 2019

Table of contents:

  1. Background
  2. OOP Concepts
  3. Implementation
  4. Testing

Background: Since Javascript is weakly typed it becomes difficult to manage large projects. We are working on a music related platform and our backend NodeJS + Express server has grown largely into many modules and sub modules. In this article, I am trying to do a POC project that help us building OOP based backend modeling.

OOP Concepts: Let us consider a simple use case where we have two types of users, Teachers and Students. Both Teacher and Students have common properties like name, email, password and common functionalities like register, login, delete.
So, Let User will become a super class and Teachers, Students will become subclass of User. Following diagram indicates the same.

1. User model diagram

Before implementing in code let us try to understand four major fundamentals of OOP.

  1. Abstraction: Abstraction means using simple things to represent complexity. Here we are representing Person as User and implementing register method. The outsider need not to know how it is implemented or how it does.
  2. Encapsulation: Encapsulation means bundling of data inside class. Here, User’s email, name are public and password, role are private. No one can access these private fields from outside of class.
  3. Inheritance: Inheritance means acquire properties and implementations from base class. Roles, Teacher and Student acquire name, email, password from the base class User and also the method register.
  4. Polymorphism: Polymorphism allows same word to mean different in different contexts. Here, we provide constructor for different use cases. For registering an user constructor should be provided with name, email, password and role. But for getting students of a Teacher we just need email.

Implementation: Let us use the typescript server that we setup in the previous article. Clone the repository into your desktop. We shall do some minor modifications in App.ts later. First we start with creating Base User class. Inside src folder create new folder with name models. Inside models create new file User.ts. User will have public properties name, email and private properties role, pwd, ID. Let us provide multiple constructors that help in creating user object at different contexts. Detailed implementation can be seen below.

/src/models/User.ts

We shall create sub classes Teacher and Student now. Create two new files Teacher.ts and Student.ts inside models folder. Teacher and Student inherits common properties and functions implemented in User. Along with that Teacher has it’s own method getStudents which returns students of that teacher. Student also has it’s own method getAssignments which returns assignments available for that student. Detailed implementation of Student and Teacher class are available below.

/src/models/Teacher.ts
/src/models/Student.ts

Now we shall use the model classes that we created. Let us three write simple API routes and simulate it from Postman. Create new folder routes inside src. Create the router file index.ts inside routes folder.

API: POST /register creates new Student or Teacher object based on roles and register that user. API will respond with newly created user. We are simply generating a integer number as ID. In real applications we save the user to Database and get the unique ID of created row or document.

API: GET /teachers/:email/students responds with array fo students that belongs to the teacher.

API: GET /students/:email/assignments responds with available assignments for that student.

Detailed implementation of the route is provided below.

/src/routes/index.ts

We have used JSONResponse class to simplify and re use the response sending functionalities. Create libs folder inside src and add below codes to JSONResponse.ts inside libs folder.

/src/libs/JSONResponse.ts

We need to load newly created route in App.ts. Along with that let us add body-parser middleware which helps in de serializing request body as JSON object. Install body-parser by command npm install body-parser — save. Detailed implementation is provided below.

/src/App.ts

Testing: Let use use Postman tool to simulate the REST APIs that we created here. You can download postman from the official page. Start the node server by command npm start.

Open the Postman and follow the instructions provided below.

POST: /register
GET: /teachers/example@email.com/students
/students/ss@email.com/assignments

Thats all!. If you find this post helpful please give claps and share. You can find more about me on linkedIn.

--

--