A simple Node.js and Express-based web service that provides APIs to generate QR codes in two formats: PNG images stored on the server and Base64-encoded strings. This service allows users to easily create QR codes from any input text.
- Generate QR Code as PNG: Takes text input, generates a QR code, saves it as a PNG image on the server, and returns the file path.
- Generate QR Code as Base64: Takes text input, generates a QR code, and returns it as a Base64-encoded string, which can be displayed directly on the frontend.
- Node.js (v14 or later)
- npm (Node Package Manager)
-
Clone the repository:
git clone https://github.com/abdelmaoulagr/qr-code-generator.git cd qr-code-generator
-
Install the required dependencies:
npm install
-
Create a directory to store the QR code images:
mkdir qr-codes
Start the server by running:
node index.js
The server will start on http://localhost:3000
.
- Generate QR Code as PNG
- Endpoint:
/generate-png
- Method:
POST
- Content-Type:
application/json
- Request Body:
{
"text": "Your text here"
}
- Response
{
"message": "QR code generated and saved",
"filePath": "/path/to/qr-code.png"
}
- Description: This endpoint generates a QR code from the provided text and saves it as a PNG file in the
qr-codes
directory on the server.
- Generate QR Code as Base64
- Endpoint:
/generate-base64
- Method:
POST
- Content-Type:
application/json
- Request Body:
{
"text": "Your text here"
}
- Response
{
"base64Data": "data:image/png;base64,..."
}
- Description: This endpoint generates a QR code from the provided text and returns it as a Base64-encoded string.
You can test the endpoints using tools like Postman or cURL.
Generate PNG QR Code:
curl -X POST http://localhost:3000/generate-png \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!"}'
Generate Base64 QR Code:
curl -X POST http://localhost:3000/generate-base64 \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!"}'