Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js or angular for handling routes in a MEAN application?

I am totally new to everything Nodejs/express/angular, and I just ran into a question that bothers me.

When you have a MEAN stack, it seems that routes can be handled by both Express.js and Angular.

Angular:

For instance, if I define a route in Angular, I can do it like this:

var app = angular.module("app", []).config(function($routeProvider) {
    $routeProvider.when('/login', {
        templateUrl: '/templates/login.html',
        controller: 'LoginController'
    });

    $routeProvider.when('/front', {
        templateUrl: '/templates/front.html',
        controller: 'FrontController'
    });


    $routeProvider.otherwise({redirectTo: '/front'})
});

But with express.js I do:

app.get('/',function(req,res){
    res.sendfile('templates/angular.html');
});

So my question is:

When do you use angular routing, and when do you use express routing?

(I might miss something very obvious here, but I hope you can point it out)

like image 726
Lars Holdgaard Avatar asked Jul 20 '14 16:07

Lars Holdgaard


People also ask

Is Express used for routing?

Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths.

What is route handler in Express JS?

Handler is a callback function that executes when a matching request type is found on the relevant route. For example, var express = require('express'); var app = express(); app. get('/hello', function(req, res){ res.

Why we use routing in Express JS?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.

What is the difference between Express and angular?

Though there is a fundamental difference between the way these frameworks are used in developing web applications, for example, Express is primarily used for backend purposes, while Angular is known for frontend capabilities.


1 Answers

Those two serve different purposes on a single page app.

The app would do all the CRUD (endpoints where you create/read/update/delete your stuff, for example: projects, users, bills, etc). Also it would do all the authentication stuff (like /login and /register).

All of that needs routes, because you would want something like /api/users to grab all your users. All those routes, AKA CRUD routes and authentication routes goes into express.js router. Why there? Because those are routes of the backend.

On the other hand, you have your angular application, which contains the visual part of your application and there you want some routes. You want / to point to your home, you would want /users to have a page where you list your users or even /users/add to have a page with a form to add new users.

You could see it this way:

Backend routes (express ones): Those are the routes that an end user won't have to know about or even use them (your angular app will use them to communicate with the backend to work with its data but an end user wouldn't put them directly on the browser)).

Frontend routes (angular ones): Are the routes that maps to different pages of your application and because of that, end users can use them to access some parts of your application directly.

like image 139
Jesus Rodriguez Avatar answered Oct 10 '22 17:10

Jesus Rodriguez