Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- ctrls/example.js +12 -0
- index.js +14 -0
- package.json +18 -0
ctrls/example.js
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
module.exports = {
|
2 |
+
path: '/example',
|
3 |
+
method: 'GET',
|
4 |
+
limit: {
|
5 |
+
windowMs: 1000*60,
|
6 |
+
max: 3,
|
7 |
+
message: 'Quá nhiều request từ IP này, vui lòng thử lại sau.'
|
8 |
+
},
|
9 |
+
handler: (req, res, next)=>{
|
10 |
+
res.send('example');
|
11 |
+
},
|
12 |
+
}
|
index.js
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const rate_limit = require('express-rate-limit');
|
3 |
+
const fs = require('fs');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const ctrls = fs.readdirSync('ctrls');
|
7 |
+
|
8 |
+
for (const file of ctrls) {
|
9 |
+
const ctrl = require('./ctrls/'+file);
|
10 |
+
|
11 |
+
app[ctrl.method.toLowerCase()](ctrl.path, !ctrl.limit?((req, res, next)=>next()): rate_limit(ctrl.limit), ctrl.handler);
|
12 |
+
};
|
13 |
+
|
14 |
+
app.listen(3000);
|
package.json
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "nodejs",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"description": "",
|
5 |
+
"main": "index.js",
|
6 |
+
"scripts": {
|
7 |
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8 |
+
},
|
9 |
+
"keywords": [],
|
10 |
+
"author": "",
|
11 |
+
"license": "ISC",
|
12 |
+
"dependencies": {
|
13 |
+
"@types/node": "^18.0.6",
|
14 |
+
"express": "^4.18.2",
|
15 |
+
"express-rate-limit": "^7.1.4",
|
16 |
+
"node-fetch": "^3.2.6"
|
17 |
+
}
|
18 |
+
}
|