Skip to content

Commit

Permalink
[Task] #34 installed and integrated tooBusy to send 503 when load is …
Browse files Browse the repository at this point in the history
…high
  • Loading branch information
Type-Style committed Feb 7, 2024
1 parent 05a726c commit bdab23a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/hpp": "^0.2.5",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@types/toobusy-js": "^0.5.4",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"axios": "^1.6.5",
Expand All @@ -44,7 +45,8 @@
"helmet": "^7.1.0",
"hpp": "^0.2.3",
"module-alias": "^2.2.3",
"raw-body": "^2.5.2"
"raw-body": "^2.5.2",
"toobusy-js": "^0.5.1"
},
"_moduleAliases": {
"@src": "dist"
Expand Down
40 changes: 24 additions & 16 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('module-alias/register');
import { config } from 'dotenv';
import express from 'express';
import toobusy from 'toobusy-js';
import compression from 'compression';
import helmet from 'helmet';
import hpp from 'hpp';
Expand All @@ -13,21 +14,26 @@ import path from 'path';
import logger from '@src/scripts/logger';

// configurations
config();
config(); // dotenv

const app = express();
app.use(
helmet({
contentSecurityPolicy: {
directives: {
"default-src": "'self'",
"img-src": "*"
}
app.use(helmet({
contentSecurityPolicy: {
directives: {
"default-src": "'self'",
"img-src": "*"
}
})
);
}
}));
app.use((req, res, next) => {
if (toobusy()) {
res.status(503).send("I'm busy right now, sorry.");
// todo add headers retry after and no cache
} else { next(); }
});
app.use(cache);
app.use(compression())
app.use(hpp());
app.use(cache);

app.use(function (req, res, next) {
if (!['POST', 'PUT', 'DELETE'].includes(req.method)) {
Expand All @@ -48,9 +54,11 @@ app.get('/', (req, res) => {
res.send('Hello World, via TypeScript and Node.js!');
});

app.get('/test', (req, res) => {
res.send('Hello Test!');
process.exit();
app.get('/test', function (req, res) {
// processing the request requires some work!
let i = 0;
while (i < 1e10) i++;
res.send("I counted to " + i);
});


Expand All @@ -60,7 +68,7 @@ app.use('/read', readRouter);
// use httpdocs as static folder
app.use('/', express.static(path.join(__dirname, 'httpdocs'), {
extensions: ['html', 'txt', "pdf"],
index: ["start.html", "start.txt"] ,
index: ["start.html", "start.txt"],
}));

// error handling
Expand All @@ -77,7 +85,7 @@ const server = app.listen(80, () => {
process.on(signal, () => {
function logAndExit() {
// calling .shutdown allows your process to exit normally
// toobusy.shutdown();
toobusy.shutdown();
logger.log(`Server shutdown on signal: ${signal} //localhost:80`, true);
process.exit();
}
Expand Down
1 change: 0 additions & 1 deletion src/scripts/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const logPath = path.resolve(dirPath, 'start.txt');

if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
console.log("path created")
}

// const logPath = path.resolve(__dirname, '../httpdocs/log', 'start.txt');
Expand Down

0 comments on commit bdab23a

Please sign in to comment.