young-ceo
young-ceo

Reputation: 5384

How can I handle angular-route with pure Node.js?

I'm learning Node.js without Express.js (trying to learn Node.js itself). For the server side, I got this:

Node.js

// Creating server
var server = http.createServer(function(request, response) {
    var filePath = false;

    // Send files for each reqeust
    if(request.url == '/') {
        filePath = 'public/index.html';
    }
    else {
        filePath = 'public' + request.url;
    }

    var absPath = './' + filePath;
    sendFile(response, cache, absPath); // Send response here
});

And for the client side, I used Angular route (not ui-route).

Angular.js:

app.config(function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
        templateUrl: '/partials/foo.html', controller: 'fooCtrl'
    });

    $routeProvider.when('/bar', {
        templateUrl: '/partials/bar.html'
    });

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

    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
});

How can I handle angular-route requests with pure Node.js? If I use Express.js, it can be done very easily - but I just want to learn Node.js first.

Upvotes: 0

Views: 251

Answers (1)

yangli-io
yangli-io

Reputation: 17334

When you call

#/bar

it links to

/partials/bar.html

as per your code.

In your server, you need to put bar.html in

/public/partials/bar.html

And it should work like magic

Upvotes: 1

Related Questions