V
V
Vitaly2015-10-08 18:23:46
JavaScript
Vitaly, 2015-10-08 18:23:46

How to allow site access to specific ip on node express server?

Under this I found the module express-ipfilter .
the description says:

// Init dependencies 
var express = require('express')
    , ipfilter = require('express-ipfilter')
    , app = express.createServer()
    ;
 
// Whitelist the following IPs 
var ips = ['127.0.0.1'];
 
// Create the server 
app.use(ipfilter(ips, {mode: 'allow'}));
app.listen(3000);

but since I'm new and just starting to figure it all out, I'm not yet able to tie it to my express :(
I have 2 files : app.js and www.js
app :
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');



var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views/'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

…..

and www :
var app = require('../app');
var debug = require('debug')('dataBase:server');
var http = require('http');


/**
 * Get port from environment and store in Express.
 */
var serverIP = '10.10.12.120'
var port = normalizePort(process.env.PORT || '3007');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port,serverIP);
server.on('error', onError);
server.on('listening', onListening);
…….

Can you please tell me how to install this module?
Or maybe it is not needed at all, and there is another, much more correct way?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
ChickenGrinder, 2015-10-08
@ChickenGrinder

This can be done anywhere where there is access to the application instance (in the code, this is the `app` variable).
It is better to connect all middleware in one place, i.e. in your case it is `app.js`

U
un1t, 2015-10-08
@un1t

express stands for nginx in the sale, in nginx it is easily configured.

V
Vitaliy, 2015-10-08
@Scorpiored88

I also found this on the net:
Returns the ip of the client that is requesting the page.
And if in routes it is trivial to check this parameter, and send certain content to a certain client. Will this be correct?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question