# ExpressJS custom middleware code ignored

**URL:** <https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764>\
**Category:** Cloud Code\
**Created:** [June 23, 2022, 8:35am UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764 "2022-06-23T08:35:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cartoonle](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/c/50afbb/32.png) [@cartoonle](https://community.parseplatform.org/u/cartoonle)\
**Post date:** [June 23, 2022, 8:35am UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/1 "2022-06-23T08:35:46Z")

</div>

Hi!

I’m trying to implement IP whitelisting to the application, only the users from the IP list should have access to the application.

I’ve tried to use the following middleware function, but the status change to 403 is ignored, status is  
always 200 and everything is working regardless of the user IP.

But i get the “not allowed…” message in log though…

Any thoughts…??

Thanks…

```auto
app.use(function (req, res, next) {

    app.set('allowedIPs', allowedIPs);

    const ip = req.headers['x-forwarded-for'] || (req.connection || {}).remoteAddress;
    console.info("IP: " + ip);
    if (allowedIPs.includes(ip)) {
        console.info("allowed...");
        return next();
    } else {
        console.info("not allowed...");
        return res.status(403).send({error: { status:403, message:'Access denied.'}});
    }
});

```

---

<div class="post-metadata">

**Author:** ![cartoonle](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/c/50afbb/32.png) [@cartoonle](https://community.parseplatform.org/u/cartoonle)\
**Post date:** [June 24, 2022, 10:58am UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/2 "2022-06-24T10:58:47Z")

</div>

Since i can’t update the original post anymore, i’m posting this reply…

I noticed, that this middleware works as it should (code above), **but only for URLs, that dont exist**!

So without that middleware i get “can’t get …” type of message, if i try to access an unexisting URL … but with this middleware i get status 403 and “access denied” message.

Why is this not working for all URLs?

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [June 27, 2022, 2:58pm UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/3 "2022-06-27T14:58:11Z")

</div>

Could you also share the code that you are using to mount this middleware and Parse Server to the Express.js app?

---

<div class="post-metadata">

**Author:** ![cartoonle](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/c/50afbb/32.png) [@cartoonle](https://community.parseplatform.org/u/cartoonle)\
**Post date:** [June 28, 2022, 5:31am UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/4 "2022-06-28T05:31:19Z")

</div>

Sure. Although i’m not mounting Parse server by myself, since i’m using commercial service ([sashido.io](http://sashido.io)).

cloud code main.js:

```auto
module.exports.app = require('./app');

```

cloud code app.js

```auto
const express = require('express');
const app = express();

// function pasted above

module.exports = app;

```

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [June 28, 2022, 1:51pm UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/5 "2022-06-28T13:51:08Z")

</div>

In this case, I’d recommend you open a ticket with them to better understand. They are probably mounting Parse Server before your custom middleware and that’s why only unmatched routes are reaching it.

---

<div class="post-metadata">

**Author:** ![cartoonle](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/c/50afbb/32.png) [@cartoonle](https://community.parseplatform.org/u/cartoonle)\
**Post date:** [June 30, 2022, 10:33am UTC](https://community.parseplatform.org/t/expressjs-custom-middleware-code-ignored/2764/6 "2022-06-30T10:33:25Z")

</div>

Ok, tnx, i’ll try that.
