After a hectic past two months, hope I get some free time in the upcomming months.

What is this?

This is a backend, written for one of my projects. This was originally developed to be a private repository, I made it public this month. And this was my first time using typescript 100 percent for backend, my experience with it is pretty cool.

Stack

  • express server
  • mongodb
  • nginx
  • pm manager

Framework

  • model
  • view
  • controller

Folder structure

NameDescription
srcSource files of the server
src/publicStatic files to be used on client side
src/modelsStoring and retrieving datails from different schema, not using mongoose models
src/controllersControllers define functions that respond to various http requests
src/server.tsEntry point for the app
src/util/helpers - logging, cache, ratelimit, database
testTest cases to perform before taking a new build, is also used in combination with github actions before merging the code to master branch
viewsTemplate files for rendering dynamic content to client side, since this is a api server, there is not much there in this folder.
.env.examplerepos.
.githubCI - github actions - run test cases on pull request and report if it is safe to merge.
cacheCache folder for responses that do not need to connect to database all the time.

Explore the code in github

Repository Link:

https://github.com/aghontpi/watchlist-backend

Misc

A cool library that I found useful for testing api

supertest

see the code examples below, link to source file

# testing routing redirect
describe('GET /asdfd/askf', () => {
  it('should return redirect', (done) => {
    request(app).get('/random').expect(302, done);
  });
});

# testing api endpoint
describe('GET /top', () => {
  it('should return type json with 200 OK', (done) => {
    request(app).get('/top').expect('Content-Type', /json/).expect(200, done);
  });
});

# testing rate limiting
describe('GET /top', () => {
  const req = request.agent(app);
  it('shoult not be ratelimited', (done) => {
    req.get('/top').expect(200, done);
  });
  it('should not be ratelimited', (done) => {
    req.get('/top').expect(200, done);
  });
  it('should not be ratelimited', (done) => {
    req.get('/top').expect(200, done);
  });
  it('should not be ratelimited', (done) => {
    req.get('/top').expect(200, done);
  });
  it('should "be ratelimited"', (done) => {
    req.get('/top').expect(429, done);
  });
});

Merging pull requests, automatically run tests on bug fixes and new features.

Nginx config used for deploying

nginx config
nginx config

pm2 managing the api

End

Hope this motivates my futureself in someway, keep learning and reach milestones constantly.