A simple REST API on Node.JS that justifies a text passed as a parameter.
This app implements a REST API using Node.JS/Typescript, it consists of two endpoints: /api/token and /api/justify. To obtain access to the /api/justify endpoint, a token must be acquired by sending a POST request to /api/token with a JSON body containing the email address. Once authenticated, sending a POST request with text content to /api/justify will return the justified text. The justification algorithm is custom-built. Any other routes will give you an 404 Not Found error.
A daily rate limit of 80,000 words is enforced, and the length of each justified text line is limited to 80 characters.
The app covers aspects such as authentication, algorithm implementation, string manipulation, Node.js environment setup (including routes, controllers, and middlewares), database management, error handling, deployment of the API, and Dockerization.
Here's the structure of the project directory
API_JustifyText/
├── docker-compose.yml
├── .env
├── README.md
└── api/
├── src/
│ ├── controllers/
│ ├── db/
│ ├── middleware/
│ ├── models/
│ ├── routes/
│ ├── utils/
│ └── server.ts
├── tests/
├── Dockerfile
├── package.json
├── package-lock.json
├── tsconfig.json
└── jest.config.js
To use this API, you can follow these steps:
- Obtain a unique Token by sending a POST request to
http://16.171.206.86:3000/api/token
with a JSON body:curl --request POST \ --url 'http://16.171.206.86:3000/api/token/?email=test%40example.com' \ --header 'Content-Type: application/json' \ --data '{ "email":"test@example.com" }'
- Make a POST request with the header "Authorization" and with the value "Bearer ${YOUR_TOKEN}" to
http://16.171.206.86:3000/api/justify
with a plain/text body as Content-Type for getting your text justified. Here's the cURL command:curl -X POST \ -H "Authorization: Bearer YOUR_TOKEN" \ -H "Content-Type: text/plain" \ -d "YOU VERY LONG TEXT YOU WANT TO BE JUSTIFIED" \ http://16.171.206.86:3000/api/justify
I recommend to use Insomnia and a generator of text like lipsum for making requests, it's easier, especially for including text content in the request body and to look at the output. It also memorizes the URL and the all the headers values.
You'll need to have docker-compose, please refer to the official Docker documentation to install it.
If you want to run locally this project on your computer, you can clone this repo:
- Clone the repo
git clone https://github.com/trobert42/API_JustifyText.git
cd API_JustifyText/
- Set the environment .env
POSTGRES_USER='your_username'
POSTGRES_PASSWORD='your_pwd'
JWT_SECRET='your_secret'
- Change the variables as you desire
const wordsLimitPerDay = 80000;
const lineLenLimit = 80;
- Build and run the containers with docker-compose
docker-compose up --build -d # "-d" flag stands for "detached" mode, it starts the services in the background
docker-compose logs -f # "-f" stands for "follow", it allows you to continuously see and follow the output of the logs from the container
Some helpful commands to stop containers and clean volumes
docker-compose -f docker-compose.yml down -v #watch out, it cleans the entire database
docker system prune -a -f --volumes
What was really challenging is for me to apply the right methods to handle the whole project. My formation is based on self-taught so every informations come from forums, videos, articles on the internet. Sometimes, some informations are pretty old or deprecated because tools are updated meanwhile or there are just better ones today.
I used Nest.Js before and this framework is a bit different for the structure project process compared to this one. But i manage to find some logic on how i should structure the folders/files and keep it simple.
One thing that was unknown to me is the sql environment, i used Prisma as a ORM before and for this little assessment, with no use of ORM tools, i had to look for sql commands to access values in my db. Thankfully, i just needed to retrieve data or updates some of it, so nothing really complex.
I find the justifyText algorithm very vague. Even on these online websites that justifies your text, it does not give everytime the same output. At first i had a problem with the ‘\n’ character because i didn't manage it. I realized a working algorithm but i know there are better and optimal ways to do it.
An another thing i don’t have are the good practices for the commits and git managing. I dont know when i have to commit or how i should write my message so its professional.
For the final project of my web school common core, I focused on authentication and API integration, so this task didn't feel new to me. I was already familiar with the Backend/Docker environment, and there's a lot of documentation available.
I enjoyed working on this small project, although I did spend a bit too much time on the text justification algorithm. Nonetheless, I was able to learn more about AWS and unit testing, which are new technologies to me.
This assessment serves as an exercise, and it is acknowledged that some aspects of the project may lack practical relevance or context.
There is one unique token which is given by the endpoint /api/token. If you lose it, you won't be able to receive it again and you can't use the same email because credentials will be taken.
This token will not expire.
Words are defined by spaces, so "-" or "123" are valids.
Every day at midnight, the word count is reset to zero. If you have 10 words left free, you can't put a request with body more than 10 words long.
If you want to follow how i managed the project throrough the days, here's my notion's page ⬅️ about this project
- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/development_environment
- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes
- https://www.youtube.com/watch?v=H9M02of22z4
- https://onlinetexttools.com/justify-text
- https://texttools.org/justify-text
- https://texttools.io/justify-text
- https://expressjs.com/en/guide/using-middleware.html
- https://www.turing.com/kb/building-middleware-for-node-js
- https://stackoverflow.com/questions/23271250/how-do-i-check-content-type-using-expressjs
- https://medium.com/@amr258144/connection-pooling-in-node-js-ea4421c72dc
- https://node-postgres.com/apis/pool
- https://www.npmjs.com/package//node-cron
- https://docs.aws.amazon.com/fr_fr/iot/latest/developerguide/creating-a-virtual-thing.html
- https://medium.com/@rajani103/deploying-nodejs-app-on-aws-ec2-instance-step-by-step-1b00f807cdce
- https://jestjs.io/docs/getting-started
- https://dev.to/nathan_sheryak/how-to-test-a-typescript-express-api-with-jest-for-dummies-like-me-4epd
I did not yet implemented all the tests. Stay tuned, i'll work on it.