I use a regular exp to match a path and route the request to pageRouter.
When I request the localhost:8000/new, the regular EXP looks not work, but I have test the EXP use /^\/(?!api$|api\/.*|resource$|resource\/.*)/.test('/new'), it return true.
What happened?
router.use('/api',apiRouter);
router.use('/resource',resourceRouter);
router.use(/^\/(?!api$|api\/.*|resource$|resource\/.*)/,pageRouter);
pageRouter.get("*",(req,resp,next)=>{let name = req.originalUrl...})
In this case, instead of regexp you just need to use the wildcard :
const express = require('express'); const app = express()
app.get('/api', function(req, res){ res.send('API·Router') })
app.get('/resource', function(req, res){ res.send('RESOURCE·Router') })
app.get('/*', function(req, res){ res.send('[*] PAGE.Router') })
app.listen(3000)
Its important to define all the other routes before the wildcard '/*'
WHY? : If your first route is the wildcard, it will trap all the requests
Detecting Paths using Wildcard
app.get('/*', function (req, res) {
var paths = req.path.split("/")
if(paths[1].length==0){
res.send('[/] Page.Root')
}else if(paths[1]=='portal'){
if(paths[2]){ res.send('[*] PAGE.Router : '+paths[2]) }
else{ res.send('[*] PAGE.Router : '+paths[1]) }
}else{ res.send('[404] PATH : '+paths[1]+' not found!') }
});
Using an array :
If you have a specific list of routes that you want to use for pageRouter you can always group them inside an array for the first argument of the app.get() handler :
const pageRoutes = ['en/page','es/pagina','dk/side','jp/頁']
app.get(pageRoutes, function(req, res){ res.send('[*] PAGE.Router') })
It seems OP is trying to exclude /api and /resource for pageRouter by regular expression. However, it's not needed. As long as /api and /resource router are defined before pageRouter, they will be triggered first, and won't go though pageRouter any more.
Just define /api and /resource before pageRouter, and then use router.use('/', pageRouter);, so that all URL except /api and /resource will be listened by pageRouter.
Here is the example code. I've verified and confirmed that HTTP request to /resource won't go through pageRouter.
Files:
./app.js
./routes.js
./pageRouter.js
./resourceRouter.js
For ./app.js:
const express = require('express');
const app = express();
const routes = require('./routes');
app.use('/', routes);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
For ./routes.js:
var express = require('express');
var router = express.Router();
const resourceRouter = require('./resourceRouter');
const pageRouter = require('./pageRouter');
router.use('/resource',resourceRouter);
router.use('/', pageRouter);
module.exports = router;
For ./resourceRouter.js:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send('respond with resource');
});
module.exports = router;
For ./pageRouter.js:
var express = require('express');
var router = express.Router();
router.get('/resource', function(req, res, next) {
console.log('hit /resource in pageRouter');
res.send('respond with page by /resource');
});
router.get('/new-page', function(req, res, next) {
res.send('respond with page by /new-page');
});
router.get('*', function(req, res, next) {
console.log('hit * in pageRouter');
res.send('respond with page by *');
});
module.exports = router;
After running ./app.js, the HTTP GET request for /resource would never hit ./pageRouter.js.
I got an answer from the expressjs's github,the link is https://github.com/expressjs/express/issues/3404
It is not just a matter of does the regular expression match the
input, but since .use needs to strip down the matched path, it expects
your match to stop before a / character to cut on.
Related
So, I have these routes in my app.js file.
app.use("/api/v1/users", userRouter)
app.use("/*", indexRouter)
And in index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/*', function(req, res, next) {
res.render('index', { title: 'Gratitude Journal App' });
});
module.exports = router;
What does the * do exactly?
It will render index page for all of your app's GET request. It (*) will match any string(route) after it.
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!');
});
How do I use multiple router files using express framework?
In my app.js, I have the following code:
var controller = require('./controller/index');
var healthController = require('./controller/health/');
app.use('/', controller);
app.use('/health', healthController);
And controller/index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
module.exports = router;
And health.js:
var express = require('express');
var router = express.Router();
/* GET health confirmation. */
router.get('/health', function(req, res, next) {
res.send('OK');
});
module.exports = router;
When I hit the http://localhost:8000/, I get the correct page without any problem, however, http://localhost:8000/health results in 404 error.
Thanks in advance.
Assuming the "health.js" resides in "controller" directory, may it be just a typo issue? var healthController = require('./controller/health/'); has a trailing slash (/). Removing it would fly? So it becomes var healthController = require('./controller/health');
Your single node app must have single router object, a router object represents a server in express requiring unique port.
Hence you should create router object in you app.js passing it to all router files.
Code will be like -
app.js
var express = require('express');
var router = express.Router();
var controller = require('./controller/index');
var healthController = require('./controller/health/');
controller(router);
healthController(router);
index.js
module.exports = function(router) {
router.get('/', function(req, res, next) {
res.render('index');
});
}
health.js
module.exports = funtion(router) {
router.get('/health', function(req, res, next) {
res.send('OK');
});
}
See How to include route handlers in multiple files in Express?.
Export an anonymous function that can be "initiated" with a reference to the original express app.
./controller/index.js:
module.exports = function(app) {
/* GET home page. */
app.get('/', function(req, res, next) {
res.render('index');
});
};
./controller/health.js:
module.exports = function(app) {
/* GET health confirmation. */
app.get('/health', function(req, res, next) {
res.send('OK');
});
};
./app.js:
var app = require('express')();
var controller = require('./controller/index');
var healthController = require('./controller/health');
controller(app);
healthController(app);
Change in health.js:
router.get('/health', function(req, res, next) {
res.send('`OK`');
});
to
router.get('/', function(req, res, next) {
res.send('OK');
});
This will work fine check it out.
I have the following app code:
(app.js)
var express = require('express')
, app = express()
, port = process.env.PORT || 8082
app.use(require('./controllers'))
app.use(function(req, res, next) {
res.send('Test')
next()
})
app.listen(port, function() {
console.log('Listening on port ' + port)
})
and two controllers:
(index.js)
var express = require('express')
, router = express.Router()
router.use('/projects', require('./projects'))
module.exports = router
(projects.js)
var express = require('express')
, router = express.Router()
router.get('/:id', function(req, res, next) {
res.json({project: req.params.id})
})
module.exports = router
This works but now I have to check my url for a valid token.
My url looks like http://server/api/projects?token=abc or http://server/api/projects/:id?token=abc
If the token is not valid no projects (or other controllers) should be load / shown.
What is the best way to handle this and where (in app.js or controllers/index.js)?
Use a middleware.
app.use(function (req, res, next) {
if (checkToken(req.query.token) {
return next();
}
res.status(403).end("invalid token");
});
app.use(require('./controllers'))
You can do this validation inside your route function or you can use a middleware that validates the param and then do some logic.
var express = require('express')
, router = express.Router()
function validate(req,res,next) {
if(req.params.id) // some logic
else other stuff
next();
}
router.get('/:id', validate, function(req, res, next) {
res.json({project: req.params.id})
})
module.exports = router