D
D
Danil2018-04-05 12:30:00
Node.js
Danil, 2018-04-05 12:30:00

How to filter users by country?

You need to make sure that only users from Russia are allowed. How to implement this? I use express.js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abigovor, 2018-04-08
@Veneomin

You need to write a middleware to filter by country,
an example using the ilocate library to get the country of the request

const express = require('express');
const app = express();
const iplocate = require('iplocate');
 
app.use(iplocate);

// middleware for filtering by country
app.use(function (req, res, next) {
  
  	// some error 
  if ( req.locationError ) {
    // handle error
  }

  if ( req.location.country_code !== 'RU' ){
    return res.send("request available only from russia")
  }

    next();
});

app
    .get('/', (req, res) => {
        res.send('success');
    })

app.listen(3000, function () {
  console.log('app listening on port 3000!');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question