Allowing access to an endpoint under a condition?

16 просмотров
Перейти к первому непрочитанному сообщению

Anton.S.

не прочитано,
31 янв. 2018 г., 16:54:1631.01.2018
– nodejs
Hello, first time posting here.

I'm a javascript and node.js newbie and would like to ask for advice.

I'm making a node server which will accept requests from a chrome extension and a certain HTTP client.
I want the server to accept requests from the extension but only under a certain condition.
What I had in mind was to use the HTTP client to send a POST request to start or stop receiving requests from the extension.
Here is what I came up with:

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

process.env.MYTOGGLE = false;

app.post('/start', function (req, res){
// HTTP client lets the server accept requests
process.env.MYTOGGLE = true;
res.send('Accepting requests from extension...');
});

app.post('/request', function (req, res){
// extension requests go here
if(!process.env.MYTOGGLE){
res.send('404 Not Found');
} else {
res.send('Thanks for the data!');
}
});

app.post('/stop', function (req, res){
// HTTP client changes the condition and requests to /request give '404 Not Found'
process.env.MYTOGGLE = false;
res.send('Not accepting requests anymore!');
});

app.listen(3000);

I've used environment variables as a demonstration to what I want to achieve in the end. 
Is there something I can use in order to store that 'toggle' variable on the server side and give the server access to it while it's running?

Or am I going about this the wrong way?

Much appreciated,
Anton


Warner Onstine

не прочитано,
31 янв. 2018 г., 17:39:0531.01.2018
– nod...@googlegroups.com
It depends on what you are toggling honestly. You could do this with environment variables, but those are only set once you deploy your app somewhere, or require a restart of the service to read them in again. Or you could set that elsewhere like a database. It really depends on what the condition of toggling on/off is as to what would determine that.

For me if it were something that I had to restart it really means it's a variable changed infrequently. From what you're saying I would stick it in the db for application config variables or something.

-warner

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/f1ee4e2b-e853-4004-aff6-3265804a9671%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zlatko

не прочитано,
1 февр. 2018 г., 15:26:3401.02.2018
– nodejs
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');
});

Warner Onstine

не прочитано,
1 февр. 2018 г., 16:07:1801.02.2018
– nod...@googlegroups.com
Oooh, I like the middleware option, good call!

-warner

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
Ответить всем
Отправить сообщение автору
Переслать
0 новых сообщений