Run this command before npm run dev or npm start
npm install -g swagger-ui-express nodemon eslint swagger express
The following functionalities are implemented in
- Authentication
- Image Thumbnail Generation
The API requires latest LTS version of Node.js preferbably __v12.14.0__ of Nodejs
To get up and running:
1. Clone the repo.
git clone https://github.com/aashishpiitk/image-thumbnail-generator
2. cd
into repo. Use the same directory name(below) if you do not change it.
cd image-thumbnail-generator
3. Setup the application by installing its dependencies with
npm install
4. Install the following packeges as glocal npm packages
npm install -g nodemon eslint swagger
5. Running the app in development mode
Run the command below from the application's root directory. The app gets up and running on port 3000 with
npm run dev
Access the server on http://localhost:3000/
6. Running the unit tests
Run the command below from the application's root directory.
npm run test
The API documentation is available in form of swagger docs at the end point
http://localhost:3000/api-docs/
You can use try it out feature in each of the endpoints in the docs to perform the request to the server and fetch the response
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 http://localhost:3000/users/login
- You will be setting 2 keys (for username and password).
Set theusername
key to any name.(minimum 3 characters long)
Setpassword
to any password (minimum of 6 characters).
Send this info in the body of request
{
"username": "aashishp",
"password": "123456"
}
- Hit
Send
. You will get a result in this format:
{
"user": "aashishp",
"authorized": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRhd2Zlc2dyIiwiaWF0IjoxNTQ5MTI2OTgwLCJleHAiOjE1NDkxMzA1ODB9.ywbMXejRhwsxg9A3QRcgPbh7bq2DnPBNTL3h2yIpaiM"
}
This request contains a public image URL. It downloads the image, resizes to 50x50 pixels, and returns the resulting thumbnail's link
- Set the request to POST and the url to http://localhost:3000/image/generate-thumbnail.
- Set the key
imageUrl
to a public image url.
Send this info in the body of request
{
"imageUrl": "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRhd2Zlc2dyIiwiaWF0IjoxNTQ5MTI2OTgwLCJleHAiOjE1NDkxMzA1ODB9.ywbMXejRhwsxg9A3QRcgPbh7bq2DnPBNTL3h2yIpaiM"
}
- The token generated earlier will be patched with the key.
- At first, if JWT is missing or invalid then the request will be rejected otherwise,
- Image will be downloaded and converted to a thumbnail of size 50x50 pixels with a sample result as below:
{
"authorized": true
"converted": true
"imagePath": "http://localhost:3000/images/resized/60thumbnail.png",
}
6.Use ctrl+mouse click to click on the imagePath which opens the resized image in browser
Unit testing is done using mochai = mocha + chai(asserion)
The ES6 code is not compatible with the LTS version of nodejs so I used Babel to transpile the ES6 code to CommonJs format
The tranpiled code can be obtained in a directory named dist in the root directory using the command npm run build