Re: [nodejs] Re: Allowing access to an endpoint under a condition?

2018-02-01 Thread Warner Onstine
Oooh, I like the middleware option, good call!

-warner

On Thu, Feb 1, 2018 at 1:51 AM, Zlatko  wrote:

> Like Warner has said, take a database. Something like redis, in this case.
> Or even a free hosted offering, if it is not mission critical. Or get a
> firebase client. In any case, some external persistent way of storing and
> retreiving this.
>
> As an aside, it would probably be better to make checking and manipulating
> this a middleware. Extract your actual "business logic" to a separate file,
> and put your MY_TOGGLE check in a middleware. It enables you to easier
> test, or e.g. later change the mechanism, or where you store the toggle or
> similar.
>
> Something like this:
>
> const app = express();
> const { getToggleState, flipToggleState } from './toggle-manager';
> app.use('/requests', async function (req, res, next) {
>  const isToggleOn = await getToggleState();
>  if (isToggleOn) {
> next();
> } else {
> res.status(403).end('Forbidden');
> }
> });
>
> app.use('/requests', yourRegularHandler);
>
> app.post('/stop', (req, res) => {
> flipToggleState();
> res.end('Ok');
> });
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules: https://gist.github.com/othiym23/9886289#file-
> moderation-policy-md
> Old group rules: https://github.com/joyent/node/wiki/Mailing-List-
> Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/fc55e8b8-6992-430e-99f6-eb9f5e8b6f12%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAJNTuMBoBApQOR0XsKK5DbP-JTus9uFMiLC-6Dnexzwn4avEGg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[nodejs] Re: Allowing access to an endpoint under a condition?

2018-02-01 Thread Zlatko
Like Warner has said, take a database. Something like redis, in this case. 
Or even a free hosted offering, if it is not mission critical. Or get a 
firebase client. In any case, some external persistent way of storing and 
retreiving this.

As an aside, it would probably be better to make checking and manipulating 
this a middleware. Extract your actual "business logic" to a separate file, 
and put your MY_TOGGLE check in a middleware. It enables you to easier 
test, or e.g. later change the mechanism, or where you store the toggle or 
similar.

Something like this:

const app = express();
const { getToggleState, flipToggleState } from './toggle-manager';
app.use('/requests', async function (req, res, next) {
 const isToggleOn = await getToggleState();
 if (isToggleOn) {
next();
} else {
res.status(403).end('Forbidden');
}
});

app.use('/requests', yourRegularHandler);

app.post('/stop', (req, res) => {
flipToggleState();
res.end('Ok');
});

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/fc55e8b8-6992-430e-99f6-eb9f5e8b6f12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.