0

I am relatively new to MEANJS, i have also been reading through its documentation to learn more.

so, i am trying to create a rest API using meanjs using its yo generator and removing the angular parts of it, so far it has been a success.

What i am trying to do now is to default the url routes to have a prefix of /api/:version, what i did so far is to append the /api/:version to the routes inside app/ like the following

//app/routes/articles.server.routes.js 
'use strict';

...

module.exports = function(app) {
    // Article Routes
    app.route('/api/:version/articles')
    ...

    app.route('/api/:version/articles/:articleId')
    ...

    // Finish by binding the article middleware
    app.param('articleId', articles.articleByID);
};

That works so far, but can i declared this somewhere so that i don't have to add /api/:version every time i create a new route?

I tried implementing the ones stated in express documentation and Remi M's answer in this stackoverflow question with no luck.

Update

meanjs installation comes with an express.js, so i thought that this is the right place to do it and i tried using the router object to solve this problem, although it doesn't work

//config/express.js
'use strict';
...
var express        = require('express');
...
var router         = express.Router();

module.exports = function(db) {
    var app = express();

    ...

    app.use(function(req, res, next) {
        res.locals.url = req.protocol + '://' + req.headers.host + req.url;
        next();
    });

    app.use(compress({
        filter: function(req, res) {
            return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
        },
        level: 9
    }));

    ...

    config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
        require(path.resolve(routePath))(app);
    });
    app.use(function(err, req, res, next) {
        if (!err) return next();
        console.error(err.stack);
        res.status(500).render('500', {
            error: err.stack
        });
    });

    app.use(function(req, res) {
        res.status(404).render('404', {
            url: req.originalUrl,
            error: 'Not Found'
        });
    });

    ...

    app.use('/api/:version', router); //this is what i add

    return app;
};
Community
  • 1
  • 1
littlechad
  • 1,202
  • 17
  • 48
  • express router is the right approach to solve this need. If you tried this and if it didn't work, post the relevant code snippet that is not working so we can help you debug the issue. – Prabhu Velayutham May 26 '15 at 05:26
  • @PrabhuVelayutham i have updated the question with the relevant snippet, please check :) – littlechad May 26 '15 at 06:46

1 Answers1

0

All of your initial configurations should be on router object and not on app For Ex :

'use strict';
var express        = require('express');
var router         = express.Router();

module.exports = function(db) {
 var app = express();


router.use(function(req, res, next) {
    res.locals.url = req.protocol + '://' + req.headers.host + req.url;
    next();
});

router.use(compress({
    filter: function(req, res) {
        return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
    },
    level: 9
}));

config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
    require(path.resolve(routePath))(router);
});
router.use(function(err, req, res, next) {
    if (!err) return next();
    console.error(err.stack);
    res.status(500).render('500', {
        error: err.stack
    });
});
router.use('/bar', function(req, res, next) {
    console.log("bar endpoint called");
    res.send("bar");
});
router.use(function(req, res) {
    res.status(404).render('404', {
        url: req.originalUrl,
        error: 'Not Found'
    });
});

  app.use('/api/:version/', router); //this is what i add

  return app;
};

Then call app.use("prefixurl",router) as above.

test the BAR API /api/v1/bar

  • tried this, i am afraid it doesn't work, please help – littlechad May 27 '15 at 05:03
  • I commented out the config.getGlobbedFiles and created a custom route as below and confirmed that it is working. Give this custom route a try. router.use('/bar', function(req, res, next) { console.log("bar endpoint called"); res.send("bar"); }); – Prabhu Velayutham May 27 '15 at 05:09
  • Here is the gist for the working code https://gist.github.com/pvela/560d8e7a044972041da2. Note that the app.use should have an ending slash and all the routes should start with a slash – Prabhu Velayutham May 27 '15 at 05:18