A simple stateless microservice in Nodejs, with three major functionalities -
- Authentication
- JSON patching
- Image Thumbnail Generation
The API requires Node.js
To get up and running:
1. Clone the repo.
git clone https://github.com/twumm/stateless-microservice-hackerbay.git
2. cd into repo. Use the same directory name(below) if you do not change it.
cd stateless-microservice-hackerbay
3. Setup the application by installing its dependencies with
npm install
4. The app gets up and running on port 3000 with npm start.
5. Important Create a .env file and set jwtSecret to any secret phrase you want.
Since this is mostly an API with post and patch requests, testing will be done with Postman
This is a mock authentication so you can pass in any username or password to login.
- Set the request to POST and the url to /api/users/login.
- In the Body for the Postman request, select x-www-form-urlencoded.
- You will be setting 2 keys (for username and password). Set the
usernamekey to any name. Setpasswordto any password (minimum of 6 characters). - Hit
Send. You will get a result in this format:
{
"user": "moi",
"authorized": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im1vaSIsImlhdCI6MTUzMjAwNDkwMSwiZXhwIjoxNTMyMDI2NTAxfQ.sonItbpZ_yKsRLDXNfDqwN6yN5VbdMVDhgKAMxDmPFY"
}
Apply json patch to a json object, and return the resulting json object.
- Set the request to PATCH and the url to /api/patch-object.
- Set the key
jsonObjectto an object you would like to patch. Set the keyjsonPatchObjectto the object you want to use to patch thejsonObject.
Examples:
jsonObject
{ "user": { "firstName": "Albert", "lastName": "Einstein" } }
jsonPatchObject
[{"op": "replace", "path": "/user/firstName", "value": "Leonardo"}, {"op": "replace", "path": "/user/lastName", "value": "da Vinci"}]
- Since this is a secure route, for testing, you will have to set the token in the
Header. Set key astokenand value as token you received from Authentication. - Expected result should be:
{ "user": { "firstName": "Leonardo", "lastName": "da Vinci" } }
This request contains a public image URL. It downloads the image, resizes to 50x50 pixels, and returns the resulting thumbnail.
- Set the request to POST and the url to /api/create-thumbnail.
- Set the key
imageUrlto a public image url. - Since this is a secure route, for testing, you will have to set the token in the
Header. Set key astokenand value as token you received from Authentication. - Image will be downloaded and converted to a thumbnail of size 50x50 pixels with a sample result as below:
{
"converted": true,
"user": "moi",
"success": "Image has been resized",
"thumbnail": "./public/images/resized/"
}
Unit testing is done using mochai.
Run npm test from the application's root directory.
All logs are saved in hackerbay.log in the application's root.
- Test for thumbnail generation with Mocha 'it should accept a public image url and return a resized image' returns a promise which is currently not being handled properly.
- Dockerfile has not been fully tested.
- Istanbul coverage not working as expected.