I have 2 router files. One is for view routing and other for api requests.
I am trying to set the routing using:
var routes = require('./routes/index'); //View Router
var api = require('./routes/api'); //API Router
app.use('/', routes);
app.use('/api', api);
This fails in case of /api requests. If I remove one of the routings, the other works.
I also tried,
routes(app);
api(app);
But this fails too. Any idea what might be the issue? Please let me know in case additional details are required.
routers/index.js
var express = require('express');
var router = express.Router();
var Promise = require('bluebird');
var nforce = require('nforce');
var org = require('../lib/connection');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'App' });
});
router.get('/accounts', function(req, res, next){
console.log(org);
res.render('partials/' + name);
org.query({query: 'Select Id, Name, Type, Industry, Rating From Account Order By LastModifiedDate DESC'})
.then(function(results){
console.log(results);
res.render('accounts', {title: 'Accounts', records: results.records});
});
});
router.get('/partials/:name', function(req, res, next){
var name = req.params.name;
console.log(name);
res.render('partials/' + name);
});
router.get('/api/:name', function(req, res, next){
var name = req.params.name;
console.log(name);
res.render('api/' + name);
});
module.exports = router;
/routers/api.js
var express = require('express');
var router = express.Router();
var Promise = require('bluebird');
var nforce = require('nforce');
var org = require('../lib/connection');
/* GET home page. */
router.get('/getAccounts', function(req, res, next) {
console.log('in API router...');
org.query({query: 'Select Id, Name, Type, Industry, Rating From Account Order By LastModifiedDate DESC'})
.then(function(results){
console.log(results);
res.json({'accounts': results.records});
});
});
module.exports = router;
Maybe wrong but i see problem in this route.
router.get('/api/:name', function(req, res, next){})
It will match /api/cuteName and it also will match /api/getAccounts.
So you need to make routes more clear. I would suggest to change route inside index.js to be all something like /main/.
And all API routes move to api.js.
Hope this helps.
Related
I am building an API backend with Express (v4) and facing an issue that my middleware function is not called
on sub-paths of my route. E.g. it is called for /movie but not for /movie/search.
I have split my routes into separate files. Below is the code, shortened to the relevant parts.
Any help is appreciated!
app.js
var express = require('express');
var app = express();
var router = require('routes')(app);
/routes/index.js
module.exports = function(app) {
app.use('/movie', check_authentication, require('movie'));
};
/routes/movie.js
var Movie = require(../models/movie');
// Middleware is working for this route (/movie?movie_id=123)
router.get('/', function(req, res) {
Movie.findById(req.query.movie_id)
.then(function(movie) {
res.status(200).json(movie);
}, function(err) {
res.status(400).send(err);
});
});
// Middleware is NOT working for this route (/movie/search?keyword=matrix)
router.get('/search', function(req, res) {
Movie.findById(req.query.keyword)
.then(function(movie) {
res.status(200).json(movie);
}, function(err) {
res.status(400).send(err);
});
});
/routes/check_authentication.js
var express = require('express');
var router = express.Router();
var firebaseAdmin = require('firebase-admin');
var path = require('path');
var config = require(path.resolve(__dirname, '../config/config.json'));
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(path.resolve(__dirname, '../config/' + config.firebase.serviceAccount)),
databaseURL: config.firebase.databaseURL
});
// AUTHENTICATION MIDDLEWARE
// needs to be included in any request which requires authorization
// =============================================================================
router.all('/', function(req, res, next) {
// check if authorization header is present
var token = req.headers['authorization'];
if (typeof token === 'undefined') {
res.status(403).json({ Error: 'Unauthenticated' });
}
else {
firebaseAdmin.auth().verifyIdToken(token).then(function(decodedToken) {
req.email = decodedToken.email;
next(); // all good. go ahead with the request
}).catch(function(error) {
res.status(403).json({ Error: 'Unauthenticated' });
});
}
});
module.exports = router;
It seems I found the problem.
Changing the middleware to trigger on * fixes it.
router.all('*', function(req, res, next)
Maybe someone can confirm that this is the way to go.
The check_authentication module should export the middleware function, not a router.
module.exports = function(req, res, next) {
// check if authorization header is present
// ...
});
I am working with Passport, and I need to pass multiple parameters through to from my controller to my router. Basically it only passes the first one.
I want to get
app.get('/auth/steam', controllers.auth.authenticate);
to result in
app.get('/auth/steam', passport.authenticate('steam'), function(req, res) { res.render('index') };);
Right now it only loads the 1st parameter.
My controller looks like this
exports.authenticate =
passport.authenticate('steam'),
function(req, res) {
res.render('index');
};
How would I do this?
EDIT: I want to only be able to call it with controllers.auth.authenticate, not in an array like: controllers.auth.authenticate[0]!
Warning NOT tested.
You can wrap all inside function
exports.authenticate = function(req, res, next) {
passport.authenticate('steam', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/auth/steam'); }
res.render("index");
});
}
Or you can use router and protect ALL verbs (get, post, etc)
var express = require('express');
var router = express.Router();
router.use(function (req, res, next) {
passport.authenticate('steam');
});
router.get('/', function(req, res, next) {
res.render("index");
});
module.exports = router;
And use router on the app
var ctrl = require("yourModuleName");
app.use('/auth/steam', ctrl); // app.use NOT app.get
Other alternative is to protect only the get
var express = require('express');
var router = express.Router();
router.get('/', passport.authenticate('steam'), function(req, res, next) {
res.render("index");
});
module.exports = router;
var ctrl = require("yourModuleName");
app.use('/auth/steam', ctrl); // app.use NOT app.get
See Express routing page
I know I can do this in Express:
app.use('/:id', function (req, res){ // or app.get
console.log('Test param: ' + req.params.id); // "Test param: foo"
});
to get my url parameter.
When I try to use a router logic I did:
index.js
var sites = require('./routes/sites');
app.use('/:id', sites);
routes/sites.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log(req.params.id); // logs "undefined"
// etc ...
});
module.exports = router;
this logic does not work, I get {} in the shell command, undefined for req.params.id.
Any idea where my logic is wrong?
It doesn't work, because that's just not how express interacts with its Routers. The reason you're not seeing id inside your router, is because :id was not declared within its scope. You'd need to do something like
app.use('/', sites);
and in your router
var express = require('express');
var router = express.Router();
router.get('/:id', function(req, res, next) {
console.log(req.params.id);
// etc ...
});
module.exports = router;
Additionally you can try playing around with app.param() see docs for examples http://expressjs.com/api.html#app.param
When you are trying to do this in index.js and routes.js -
app.use('/:id', function (req, res){ // or app.get
console.log('Test param: ' + req.params.id); // "Test param: foo"
});
router.get('/', function(req, res, next) {
console.log(req.params.id); // logs "undefined"
// etc ...
});
"id" is not defined there because that's how express works with routers, it just matches the routes matching with the specified prefix in app.use() method. For that, you can try using -
const express = require('express');
const router = express.Router({mergeParams: true});
Now it will work perfectly.
It actually works now.
You can do something like
app.use('/:id/transactions', require('./routes/transactions'));
In the main file, and then add {mergeParams:true} to Router options in the controller file.
const router = require('express').Router({mergeParams:true});
So here you will be able to get the id that was set in base Route.
// localhost:5000/:id/transactions/
router.get('/',(req, res) => {
console.log(req.params.id);
}
Found here:
http://expressjs.com/api.html#app.param
In your file router/sites.js, add an option to express router:
const router = express.Router({mergeParams:true});
Now you have access to id in sites.js file.
Without resorting to regular expression is there anyway with expressjs to recursively call routing ie url examples:
/f:forum/s:section/t:thread/p:post
/f:forum/s:section/s:section/t:thread/p:post
/f:forum/s:section/s:section/s:section/t:thread/p:post
...
Therefore allowing technically an infinite amount of "sections/subsections" in the forum.
I attempted to do:
app.js:
var express = require('express');
app = express();
app.route('/').get(function(req, res, next){
return res.send('hello');
});
app.use('/f:forum', require('./section'));
server = app.listen(process.env.http || process.env.PORT);
module.exports = app;
section.js:
var router = require('express').Router();
router = router;
router.route('/s:section').get(function(req, res, next){
return res.send(req.params);
});
router.use('/s:section', require('./thread'));
module.exports = router;
thread.js:
var router = require('express').Router();
router.use('/s:section', require('./section'));
router.route('/t:thread/p-:post').get(function(req, res, next){
return res.send(req.params);
});
router.route('/t:thread').get(function(req, res, next){
return res.send(req.params);
});
module.exports = router;
but interestingly it tells me that in thread.js require('./section') = {}
yet in app.js it is correct... any suggestions?
You can do wildcard routing like router.route('/:path*') and then have the handler parse from that point down.
For example, something like:
router.route('/forum/:path*', function(req,res){
var requestPath = req.path; // will present the whole path to you for parsing
// do whatever db lookup logic you normally would do now that you have the pieces you wanted
res.render('forum', data);
};
Im trying to use a param in a route... tried the 4.x docs on express website but no use.. im still getting 404... seems like its not hitting my function..
The other routes to create and list all itens are fine..
var db = require('../db');
var express = require('express');
var mongoose = require( 'mongoose' );
var Users = mongoose.model( 'Users' );
var app = express.Router();
//PARAM
app.param('name', function(req, res, next, name) {
Users.find({name:name}, function(err, docs) {
if (err) {
return next(err);
}
else if (!name) {
return next(new Error('failed to load user'));
}
req.user = docs[0];
next();
});
});
//SHOW
app.get('/users/:name', function(req,res){
res.render('users/profile', {user: req.user});
});
tried a simple version... same 404... (btw the name exist.. it shows on the list item)
//SHOW
app.get('/users/:name', function(req, res, name){
var name = req.param('name');
Users.find({name: name}, function(err, docs) {
req.user = docs[0];
next();
});
res.render('users/profile', {user: req.user});
});
It's because you're using an Express 4 Router like it's an Express 3 app. A Router lets you set up a collection of related routes, like you have done. However, you then must attach the Router to your larger Express server.
var express = require('express');
var app = express();
var router = express.Router();
// attach some things to router
// router.get('/users/:name', ...)
app.use('/foo', router);
Would mount all the paths that router handles under the /foo path, like /foo/users/:name.