Skip to content

Commit bdcea96

Browse files
committed
add default value to the routeCheck middleware
1 parent 59fbd01 commit bdcea96

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

app.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/**
33
* Takes in the **express()** function
44
* @param {Object} app - The **express()** function
5-
* @param {Object} [opt] - The options
6-
* @param {String} [opt.path] - The redirect path
75
* @example //For the option
8-
{ path: '/PnF' }
6+
{ redirectPath: '/PnF' }
97
*
108
* @returns {function():void}
119
*/
1210

13-
const routeCheck = (app, opt) => {
11+
const routeCheck = (app, {
12+
redirectPath = '',
13+
} = {}) => {
1414

1515
//Error checking
1616
if (!app._router) {
@@ -63,10 +63,10 @@ const routeCheck = (app, opt) => {
6363
//Redirect to 404 PnF if match failed
6464
if (!pathExists) {
6565

66-
if (!opt) {
66+
if (!redirectPath) {
6767
return res.sendStatus(404);
6868
}
69-
return res.redirect(opt.path);
69+
return res.redirect(redirectPath);
7070
}
7171

7272
//If all good
@@ -84,7 +84,7 @@ const emptyInputCheck = ({
8484
checkGet = true,
8585
emptyBodyMsg = 'The request body is empty!',
8686
emptyFieldMsg = 'Some fields are missing!',
87-
}) => {
87+
} = {}) => {
8888

8989
return (req, res, next) => {
9090

test/main.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @jest-environment node
33
*/
4-
54
const { server, } = require('./server');
65
const axios = require('axios').default;
76

test/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const express = require('express');
33
const bodyParser = require('body-parser');
44
const { routeCheck, emptyInputCheck, } = require('../app');
5+
56
//Global Constant
67
const PORT = 5003;
78

@@ -48,7 +49,7 @@ app.post('/', (req, res) => {
4849
});
4950

5051
//The routeCheck middleware
51-
app.use(routeCheck(app, { path: '/PnF', }));
52+
app.use(routeCheck(app, { redirectPath: '/PnF', }));
5253

5354

5455
const server = app.listen(PORT);

0 commit comments

Comments
 (0)