MEANJS User Profile Functionality

I am trying to create a simple application using meanjs. I have 2 modules: a standard user module and a message module. what I want to achieve is a user profile page that will display some information about specific users and lists belonging to that user. you can think of it as a twitter profile page. I thought this would also be the best / users / username URL structure pattern. but I'm pretty new to all of this and am stuck now. here is what i have done so far:

I added these lines to users.server.routes.js:

app.route('/api/users/:userName').get(users.userByUsername);

app.param('userName', users.userByUsername);

added a function to manage the server as follows:

exports.userByUsername = function(req, res, next, username) {
    User.findOne({
        username: username
    }).exec(function(err, user) {
        if (err) return next(err);
        if (!user) return next(new Error('Failed to load User ' + username));
        req.profile = user;
        next();
    });
};

I added this part to users.client.routes.js

state('view.profile', {
                url: '/users/:username',
                templateUrl: 'modules/users/views/view-profile.client.view.html'
            });

i created a new view like this:

<section class="row" data-ng-controller="ViewProfileController" ng-init="findUser()">
    <div class="col-xs-offset-1 col-xs-10 col-md-offset-4 col-md-4">
        ...        

    </div>
</section>

and finally, I created a new client controller:

angular.module('users').controller('ViewProfileController', ['$scope', '$http', '$stateParams', '$location', 'Users', 'Authentication',
    function($scope, $http, $location, Users, Authentication, $stateParams) {
        $scope.user = Authentication.user;


        $scope.findUser = function () {

             $scope.ourUser = Users.get({
                username: $stateParams.username
            });

        };
    }
]);

/ , . - , ? .

+4
1

, , , . , , . . .

URL .../user/username, users.server.route.js.

app.route('/api/users/:username').get(users.read);
app.param('username', users.userByUsername);

userByUsername users.profile.server.controller.js:

/*
* user middleware
* */
exports.userByUsername = function(req, res, next, username) {
User.findOne({
    username: username
}, '_id displayName username created profileImageURL tagLine').exec(function(err, user) {
    if (err) return next(err);
    if (!user) return next(new Error('Failed to load User ' + username));
    req.user = user;
    next();
});
};

tagLine, . . . , , .. . , .

, .../api/users/username , json. , , . - .

posts.server.routes.js:

app.route('/api/posts/of/:userid').get(posts.listOf);

... posts.server.controller.js:

exports.listOf = function(req, res) { Post.find( { user: req.params.userid }).sort('-created').exec(function(err, posts) {
if (err) {
    return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
    });
} else {
    res.jsonp(posts);
}
});
};

, .../api/posts/of/userid , json .

, REST.

users.client.routes.js :

state('users', {
            url: '/users/:username',
            templateUrl: 'modules/users/views/view-profile.client.view.html'
        });

URL- html , . client/views:

<div class="col-md-12" data-ng-controller="ViewProfileController" data-ng-init="findUser()">
...
{{ ourUser }}
<hr />
{{ userPosts }}
....

. . , ViewProfileController. findUser . :

'use strict';

angular.module('users').controller('ViewProfileController', ['$scope', '$http', '$location', 'Users', 'Authentication', '$stateParams', 
function($scope, $http, $location, Users, Authentication, $stateParams) {
    $scope.user = Authentication.user;

    $scope.findUser = function () {

       $scope.ourUser = Users.get({
            username: $stateParams.username
        }).$promise.then( function(ourUser) {
               var userPostsPromise = $http.get('api/posts/of/' + ourUser._id);
               userPostsPromise.success(function(data) {
                   $scope.userPosts=data;
                   $scope.ourUser=ourUser;
               });
               userPostsPromise.error(function() {
                   alert('Something wrong!');
               });
           }
       );
    };

}
]);

angular . , AJAX, . , .. , .

. , view-profile.client.view . ourUser userPosts . URL- : .../users/username.

, .

+4

All Articles