A simple stateless microservice in Nodejs, with two major functionalities - Authentication and Image Thumbnail Generation
The API requires Node.js
To get up and running:
1. Clone the repo.
git clone https://github.com/sugam45/Microservices.git
2. cd
into repo. Use the same directory name(below) if you do not change it.
cd Microservices
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 Edit the env
variables in nodemon.json file and set jwtSecret
to any secret phrase you want.
4. Make sure to also add your Mongo Atlas Admin Username to a nodemon.json file and change the moongoose.connect
address in app.js:11 to your needs.
{
"env": {
"MONGO_ATLAS_PW": "ATLAS_PW",
"JWT_KEY": "KEY"
}
}
Since this is mostly an API with post 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/user/signup.
- In the Body for the Postman request, select raw with JSON format.
- You will be setting 2 keys (for email and password). Set the
email
key to any name. Setpassword
to any password. - Hit
Send
. You will get a result in this format:
{
"message": "Signup sucessful"
}
- The email id and password(after getting hashed by bcrypt) will be stored in an online MongoDB database.
- For login Set the request to POST and the url to /api/user/login.
- In the Body for the Postman request, select raw with JSON format.
- You will be setting 2 keys (for email and password). Set the
email
andpassword
to the previously assigned values. - Hit
Send
. You will get a result in this format:
{
"message": "Auth successful",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAdGVzdDEuY29tIiwidXNlcklkIjoiNWVjZDEwNWM5MDBhYmQ4Y2I0MTAzMDFmIiwiaWF0IjoxNTkwNDk3Mzc4LCJleHAiOjE1OTA1MDA5Nzh9.OdQcXpdZcWKIZ6xrERpGwULExS2oi2RwxqO7itW6scw"
}
This request resizes to an image to 50x50 pixels, and returns the resulting thumbnail as well as the originol image.
- Set the request to POST and the url to /api/products.
- Since this is a secure route, for testing, you will have to set the token in the
Header
. Set key asAuthorization
and value as token you received from Authentication. - In the
Body
, select form-data format and set the key asproductImage
with File and browse to the image you want to genrate a thumbnail of. - The Image will be then converted to a thumbnail of size 50x50 pixels with a sample result as below:
{
"message": "Uploaded Image successfully",
"createdProduct": {
"productImage": "uploads/pexels.jpeg"
}
}