strisunshine
strisunshine

Reputation: 399

Can I make use of AngularJS's routing configuration inside Express?

I have the following code on Angular side: app.js

 angular.module('myApp',[
    'ngRoute'
  ])
  .config(function ($routeProvider, $locationProvider) {
    $routeProvider
      .when('/', {
        redirectTo: '/signin'
      })
      .when('/signup', {
        templateUrl: 'app/account/signup/signup.html',
        controller: 'signupController',
        controllerAs: 'ctrl'
      })
      .when('/signin', {
        templateUrl: 'app/account/signin/signin.html',
        controller: 'signinController',
        controllerAs: 'ctrl'
       })
     })

And following code on my express side: server.js

var express = require('express');
var app = express();
var path = require("path");
var router = express.Router();
router.get('/', function(req, res){
    res.status(200).sendFile(path.join(__dirname + '/index.html'));
});
app.listen(8000);

signup.html and signin.html are ng-views inside index.html.

Currently I'm only being able to serve one static page. Is there any way that we're able to use the angular route configuration? Or I must write separate routings in express one more time?

Upvotes: 0

Views: 125

Answers (2)

strisunshine
strisunshine

Reputation: 399

Use npm to install http-server package instead of manually creating the server would take advantage of the Angular routing.

Upvotes: 0

Randy
Randy

Reputation: 4391

If I understand you correctly, you just want a way to serve out static files. This line will do just that.

app.use(express.static(path.join(__dirname, 'public')));

It's a middleware which will check ./public/ for any files which matches the request. If a match is made it will return that file. If no files match then it'll continue on and you can handle it however you want later. So in your case you'll see to create this folder path public/app/account/signin/ and put signin.html in it and then create public/app/account/signup/ and signup.html in it.

Upvotes: 1

Related Questions