0

I am using Node for back end Angular for front end but what will happen if will declare same route for front end and backend. I haven't tried it yet.

E.g: If I am building a TODO app and if I have /todos back end service and I am rendering todos view with same route using angular.

3 Answers 3

3

AngularJS is processing the route after the # by default. So nothing will happen if you don't change that.

Otherwise, the backend route will be called.

Sign up to request clarification or add additional context in comments.

Comments

1

Angular is client side browser framework. Your app defaults to slash(/) which points to your index.html. Your routes prefixed with hash(#) which prevents browser to make requests to server.

Angular defaults to client side & use its own routing mechanism. Express provides server side RESTful routes which behaves like an REST Api for your angular app.

In case if you want to use HTML5 Pushstate API(removes hash(/)) from default angular routing mechanism, the only thing which separates angular routes and express/server routes, You just need to structure your app like below.

  express()
        .use('/api', backend) // backend is express app
        .use('/', www) // www is public/static files
        .all('/*', function (req, res, next) {
            "use strict";
            // Just send the index.html for other files to support HTML5Mode
            res.sendfile('./app/index.html', {root: __dirname});
        })
        .listen(process.env.PORT || 8888, function () {
            debug('Express dev server listening on port ');
        });

Your express/server routes lies after /api part, and other routes(obviously your angular routes) will return index.html(html snapshot).

Above mechanism is mostly preferable for MEAN web apps.

Comments

0

Angular provides its own routes after # like myhost.io/angular/#/someroute while node provides ordinar routes like myhost.io/some/other/route witch makes almost impossible to fail!

NOTE:

But you must be careful with setting routes, because if you have static files (like angular client), your routes with same path won't work using express or connect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.