Skip to content

Commit

Permalink
[Task] #18 replaced getRawBody with builtIn express urlEncoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Feb 16, 2024
1 parent 14ca99d commit d077414
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 31 deletions.
87 changes: 66 additions & 21 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
"dependencies": {
"chalk": "^4.1.2",
"compression": "^1.7.4",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-rate-limit": "^7.1.5",
"express-slow-down": "^2.0.1",
"express-validator": "^7.0.1",
"helmet": "^7.1.0",
"hpp": "^0.2.3",
"module-alias": "^2.2.3",
"raw-body": "^2.5.2",
"toobusy-js": "^0.5.1"
},
"_moduleAliases": {
Expand Down
15 changes: 6 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import toobusy from 'toobusy-js';
import compression from 'compression';
import helmet from 'helmet';
import hpp from 'hpp';
import getRawBody from 'raw-body';
import cache from './middleware/cache';
import * as error from "./middleware/error";
import writeRouter from '@src/controller/write';
Expand All @@ -18,6 +17,8 @@ config(); // dotenv

const app = express();

app.set('view engine', 'ejs');

app.use((req, res, next) => { // monitor eventloop to block requests if busy
if (toobusy()) {
res.status(503).set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Retry-After': '60' }).send("I'm busy right now, sorry.");
Expand All @@ -28,7 +29,7 @@ app.use((req, res, next) => { // clean up IPv6 Addresses
res.locals.ip = req.ip.startsWith('::ffff:') ? req.ip.substring(7) : req.ip;
next();
} else {
const message = "No IP provided"
const message = "No IP provided";
logger.error(message);
res.status(400).send(message);
}
Expand All @@ -38,16 +39,12 @@ app.use(helmet({ contentSecurityPolicy: { directives: { "default-src": "'self'",
app.use(cache);
app.use(compression())
app.use(hpp());
app.use(function (req, res, next) { // limit request size limit when recieving data
if (!['POST', 'PUT', 'DELETE'].includes(req.method)) { return next(); }
getRawBody(req, { length: req.headers['content-length'], limit: '1mb', encoding: true },
function (err) { if (err) { return next(err) } next() }
)
})
app.use(express.urlencoded({ limit: '0.5kb', extended: true }));


// routes
app.get('/', (req, res) => {
console.log(req.ip + " - " + res.locals.ip);
logger.log(req.ip + " - " + res.locals.ip, true);
res.send('Hello World, via TypeScript and Node.js! ' + res.locals.ip);
});

Expand Down

0 comments on commit d077414

Please sign in to comment.