Please, i need to figure out why the external callback(defined in a diff file) assigned to a route like
app.get('/list', routes.list);
it's working and if I define
var router = express.Router();
router.get('/list', routes.list);
the callback stops to work.
Thanks.
You should apply routes for your application, for example
var routes = {
list: function (req, res, next) {
res.sendFile(path.join(__dirname, './public', 'index.html'));
}
};
// app.get('/list', routes.list);
router.get('/list', routes.list);
// apply the routes to our application
app.use('/', router);
app.listen(3000);
Related
Below code is from http://expressjs.com/en/guide/using-middleware.html#middleware.router. It defines a middleware on express router instance. It works fine but if I define another router and that router will also use the same middleware. Can I define a middleware only for a particular express.Router() instance?
var app = express()
var router = express.Router()
// predicate the router with a check and bail out when needed
router.use(function (req, res, next) {
if (!req.headers['x-auth']) return next('router')
next()
})
router.get('/', function (req, res) {
res.send('hello, user!')
})
// use the router and 401 anything falling through
app.use('/admin', router, function (req, res) {
res.sendStatus(401)
})
To use something specific to a route you can use the .all() function
router.route('/')
.all(function (req, res, next){
// do middleware stuff here and call next
next();
})
.get(function (req, res) {
res.send('hello, user!');
});
I have the following express server setup:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
console.log('/*');
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/test', function (req, res) {
console.log('test');
//res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
Everytime I hit localhost:9000/test, it still take to the route '/'.
I tried placing the app.get code before the static declaration but I don't think that really makes any difference. Any idea why this is happening?
Your existing code is fine. It should work.
I believe you had your index route as app.get('/*', function (req, res){ instead of app.get('/', function (req, res) { before and that's why it was capturing all requests.
In the /test route handler you have to terminate the request-response cycle by using res.end(), something like this:
app.get('/test', function (req, res) {
console.log('test');
res.end();
});
Otherwise the page will never refresh showing the first page (route /)
Hope this helps
Is it possible to apply different ip filters to different routes?
For example, I want only people from 123.123.123.123 can access my server's /test route, and only people from 124.124.124.124 can access my server's / route.
I know that express-ipfilter can restrict site access by IP Address. But it cannot apply the filter to specific routes.
I also know that adding app.use(ipfilter(ips, {})); in the middle of the routes can apply filter only to the routes below:
var express = require('express'),
ipfilter = require('express-ipfilter').IpFilter;
var ips = ['::ffff:127.0.0.1'];
var app = express();
app.get('/test', function(req, res) {
res.send('test');
});
app.use(ipfilter(ips, {})); // the ipfilter only applies to the routes below
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(3000);
But I want different filters for different routes.
Is it possible to do this?
Yeah, it's possible. You could do something like:
app.get('/test', function(req, res){
var trustedIps = ['123.123.123.123'];
var requestIP = req.connection.remoteAddress;
if(trustedIps.indexOf(requestIP) >= 0) {
// do stuff
} else {
// handle unallowed ip
}
})
You may need to make sure that requestIP is correctly formatted though.
Warning: package express-ipfilter is now deprecated.
You can chain middlewares (and ipFilter is a middleware). There are 2 ways to do this:
var express = require('express'),
ipfilter = require('express-ipfilter').IpFilter;
var ips = ['::ffff:127.0.0.1'];
var testers = ['1.2.3.4'];
var app = express();
app.get('/test', ipfilter(testers, {mode: 'allow'}), function(req, res) {
res.send('test');
});
// the ipfilter only applies to the routes below
app.get('/', ipfilter(ips, {mode: 'allow'}), function(req, res) {
res.send('Hello World');
});
app.listen(3000);
Or qualify the use of the middleware:
var express = require('express'),
ipfilter = require('express-ipfilter').IpFilter;
var ips = ['::ffff:127.0.0.1'];
var testers = ['1.2.3.4'];
var app = express();
app.use('/test', ipfilter(testers, {})); // the ipfilter only applies to the routes below
app.get('/test', function(req, res) {
res.send('test');
});
app.use('/', ipfilter(ips, {})); // the ipfilter only applies to the routes below
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(3000);
In your main file where u defined app,
app.use('/test',require('./whereever-my-route-is-located-where /test routes '));
app.use('/',require('./wherever-my-this-routes-are-located'))
in your route file .
var express = require('express'),
router = express.Router();
//Ip verification for all requests : for whereever-my-route-is-located-where /test routes
router.use(function(req, res, next) {
//verify Ip Logic
});
//this will be called for every route u define in that file, if it fails.
Lets say I want to have 2 different instances in "subfolders" in the url. In app js I have it defined like this:
var routes = require('./routes/index');
app.use('/myapp1', routes);
app.use('/myapp2', routes);
The inner routing would be the same.
But still in the router I want to "get" the path defined in the app.use - eg.: myapp1, myapp2
How do I get this in the router?
From routes/index.js:
router.use(/\/.*/, function (req, res, next) {
// want to see "myapp1/myapp2" without the *sub* path defined in this particular router eg.: /products /user etc.
next();
});
You might want to use the req.baseUrl property.
Example:
routes.get('/1', function(req, res) {
res.send([
req.baseUrl,
req.path,
req.baseUrl + req.path,
].join('\n'));
});
app.use('/api', routes);
Making an HTTP request to /api/1 would print:
/api
/1
/api/1
var express = require('express');
var app = express();
var router = express.Router();
app.use(function(req, res, next) {
req.appInstance = (req.url.indexOf('/app2/') == 0) ? 2 : 1;
next();
});
app.get('/', function(req, res, next) {
res.redirect('/app1/user');
});
router.get('/user', function(req, res, next) {
res.send(req.url +' on app' + req.appInstance);
});
app.use('/app1', router);
app.use('/app2', router);
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
I have an app with following code for routing:
var router = express.Router();
router.post('/routepath', function(req, res) {});
Now I have to put routing code in different files so I tried to use this approach, but it is not working perhaps because instead of express.Router() it uses:
app.post("/routepath", function (req, res) {});
How can I put routing in different files using express.Router()?
Why app.get, app.post, app.delete, etc, are not working in app.js after using express.Router() in them?
Here's a simple example:
// myroutes.js
var router = require('express').Router();
router.get('/', function(req, res) {
res.send('Hello from the custom router!');
});
module.exports = router;
// main.js
var app = require('express')();
app.use('/routepath', require('./myroutes'));
app.get('/', function(req, res) {
res.send('Hello from the root path!');
});
Here, app.use() is mounting the Router instance at /routepath, so that any routes added to the Router instance will be relative to /routepath.