我正在开发一个节点JS / 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
非常感谢您的回答!
我认为你的所有get请求都映射到这段代码
app.get('*', (req, res) => {
enter code here`res.sendFile('index.html', { root: publicweb });
});
这就是为什么你在查询服务器中获得index.html
的原因。
并且app.use()
将被用于应用中间件,所以在中间件工作结束后,app.get()
方法被执行,所以你得到index.html