-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakub Węglarek
committed
Jan 29, 2021
0 parents
commit 6fc3015
Showing
8 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.git | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <https://unlicense.org> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# RandomAPI | ||
RandomAPI is a super simple express API generating random numbers | ||
# Usage | ||
### URL queries | ||
1. `min` (required) - min random number (inclusive) [must be >= 0 & < max] | ||
1. `max` (required) - max random number (inclusive) [must be > min] | ||
1. `num` (optional) - amount of random number to generate (default: 1e5)<br /><br /> | ||
Feel free to edit `config/config.json` to change: server port, request path, `num` limit | ||
### HTTP Headers | ||
**Request**<br /> | ||
`GET /?`min`=1&`max`=10&`num`=100 HTTP/1.1`<br /><br /> | ||
**Response**<br /> | ||
`HTTP/1.1 200 OK`<br /> | ||
### Response body | ||
*Randomly generated array of numbers:* | ||
<br /> | ||
`[0,9,1,9,5,...0,9,4,4,6]`<br /><br /> | ||
The amout of numbers is defined in the `num` request header | ||
# Changelog | ||
29.01.2021. - Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"port": 80, | ||
"path": "/", | ||
"numLimit": 1e5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = (response, code) => { | ||
switch (code) { | ||
case 400: | ||
response.status(400).end("Bad Request", "utf-8"); | ||
break; | ||
case 416: | ||
response.status(416).end("Range Not Satisfiable", "utf-8"); | ||
break; | ||
case 500: | ||
response.status(500).end("Internal Server Error", "utf-8"); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const config = require("../config/config.json"); | ||
const sendResponse = require("../functions/sendResponse.js"); | ||
module.exports = (request, response) => { | ||
response.set({ | ||
"Access-Control-Allow-Origin": "*" | ||
}); | ||
if (!request.query.min) sendResponse(response, 400); | ||
else if (!request.query.max) sendResponse(response, 400); | ||
else if (isNaN(request.query.min)) sendResponse(response, 400); | ||
else if (isNaN(request.query.max)) sendResponse(response, 400); | ||
else if (request.query.min < 0) sendResponse(response, 416); | ||
else if (request.query.min > request.query.max) sendResponse(response, 416); | ||
else if (!request.query.num) { | ||
const min = request.query.min; | ||
const max = request.query.max; | ||
const random = Math.floor(Math.random() * (max - min + 1) + min); | ||
response.status(200).end(random.toString()); | ||
} else { | ||
if (isNaN(request.query.num)) sendResponse(response, 400); | ||
else if (request.query.num < 1) sendResponse(response, 416); | ||
else if (request.query.num > config.numLimit) sendResponse(response, 416); | ||
else { | ||
const min = request.query.min; | ||
const max = request.query.max; | ||
const numbers = []; | ||
for (let i = 0; i < request.query.num; i++) { | ||
const random = Math.floor(Math.random() * (max - min + 1) + min); | ||
numbers.push(random); | ||
} | ||
response.status(200).end(JSON.stringify(numbers)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const express = require("express"); | ||
const handleRequest = require("./handlers/request.js"); | ||
const config = require("./config/config.json"); | ||
const app = express(); | ||
app.get(config.path, handleRequest); | ||
app.listen(config.port, () => console.log(`The server has started listening on port: ${config.port}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "randomapi", | ||
"version": "1.0.0", | ||
"description": "RandomAPI is a super simple express API generating random numbers", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Wengiel", | ||
"license": "Unlicense", | ||
"dependencies": { | ||
"express": "^4.17.1", | ||
"fs": "^0.0.1-security" | ||
} | ||
} |