首页 文章

Angular 6 / Express中的路由错误

提问于
浏览
0

我正在开发nodeJS / Angular 6 / Express应用程序 . 快递只有1条路线用于“后端”,许多路线用于角度 .

我在本地运行没有问题,但是当我尝试在Azure上部署它时,Angular路由工作正常,但不是后端路由(将我重定向到根 url ) .

我认为Angular优先考虑后端路由 .

这是一些文件:

server.js :

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fetch = require('node-fetch');
const publicweb = './dist/forms';
const app = express();
app.use(express.static(publicweb));


app.use('/api/test', (req, res) => {
   res.send("test");
});

app.get('*', (req, res) => {
  res.sendFile('index.html', { root: publicweb });
});

const port = '1337';
app.listen(port, () => console.log(`API running on localhost:${port}`));

web.config :

<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
  <rewrite>
    <rules>
            <rule name="Express.js URIs">
      <match url="api/*" />
      <action type="Rewrite" url="server.js" />
    </rule>
      <rule name="Angular" stopProcessing="true">
        <match url="/*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" 
         negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>

package.json :

"start" : "node src/server.js"

app-routing.module.ts :

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { RetailComponent } from './retail/form/retail.component';

const routes: Routes = [
  {
    path: '',
    redirectTo: '/',
    pathMatch: 'full'
  },

 {
    path: 'retail',
    component: RetailComponent
 }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {

}

When I deploy that app on Azure, and I try to access /api/test

My build definition in Azure

非常感谢您的回答!

1 回答

  • 0

    我认为你的所有get请求都映射到这段代码

    app.get('*', (req, res) => {
        enter code here`res.sendFile('index.html', { root: publicweb });
    });
    

    这就是为什么你在服务器的查询中得到 index.html .

    并且 app.use() 将用于应用中间件,因此在中间件工作结束后 app.get() 方法被执行,因此您获得index.html

相关问题