I'm learning MEAN.JS and having a few issues with routing, just looking to add a new function to the articles project called getSubArticles which calls to the server and does something.
//articles.server.routes.js
app.route('/articles/:articleId/subarticles')
.get(articles.read);
//articles.server.controller.js
exports.read = function(req, res) {
res.jsonp("My Sub articles");//just using for testing
};
//articles.client.controller.js
$scope.getSubArticles = function() {
// What goes here as everything I've tried has failed, but the route could be constructed incorrectly
};
//articles.client.view.html
<section id="subarticle-listing" data-ng-controller="ArticlesController" data-ng-init="getSubArticles()">
Any ideas where I'm going wrong? Got a sample MEAN.JS project with non default routes/functions I can look at...
Cheers, Adrian
A few things jump out, first you should make sure your route is getting hit when requested, you can add a console.log in your read funciton. In your app.js (or server.js, whichever file is used to start your app) you need to have:
//server.js file:
var router = express.Router();
var routes = require('./articles.server.routes.js'); //path to your routes file
router.use('/',routes);
In your articles.server.routes.js file, include these requires at the top, and your route can't have a variable then a fixed name afterwards, basically anything after the variable will get chopped off, so the new route should be:
// articles.server.routes.js
var express = require('express'),
app = express().Router();
app.route('/articles/subarticles/:articleId')
.get(articles.read);
In your articles.server.controller.js:
exports.read = function(req, res) {
var articleid = req.params.bid
console.log('read function called' + articleid)
res.jsonp("My Sub articles");//just using for testing
};
Now in your Angular controller:
$scope.getSubArticles = function() {
$http.get('/articles/subarticles/' + $scope.articleId)
.success(function(data){
//do something with your return data
}
.error(function(err){
//error handler
}
}
Hope that helps, if not, let me know and I can suggest a few more things.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.