Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Express + Angular routing bug

I am working on learning authentication using Angular 1 and Express. I've got my backend set up for testing purposes, and i'm now working on the front end. I've got basic routes set up.

This is the core to my front end:

function router($stateProvider, $urlRouterProvider, $locationProvider) {
  $urlRouterProvider.otherwise('/');

  $stateProvider
    .state('home', {
      url: '/',
      templateUrl: 'views/home/',
      controller: 'mainController'
    })
    .state('login', {
      url: '/login',
      templateUrl: 'views/login/',
      controller: 'loginController'
    });

  $locationProvider.html5Mode(true);
}

And here are the routes on the back end that would be relevant:

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

app.get('/', function(req,res){
  res.sendFile(path.join(__dirname, 'public/', 'index.html'));
});

The only other routing I have is for my API, which use the constant apiRouter and inject /api/ into those routes.

Now, the issue comes when I try to access a route directly from the browser. I've I nagivate to localhost:8080/login I get the error Cannot GET /login, but using a ui-sref on my index page, it seems to work just fine.

Any ideas?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

So I figured out what my issue was.

After my root path GET statement in my server file, I need to add the following code:

app.get('*', function(req,res){
    res.sendFile(path.join(__dirname, 'public/', 'index.html'));
});

What was happening, is that Express was trying to find a /login route that I had defined. The problem is, that route needs to be handled by Angular, and not Express, so the '*' is a catch-all for any route that Express doesn't know how to explicitly handle, it then hands off that responsibility to the index.html file.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda