部署在Azure App Service的React+Vite+Express(Node.js)应用/hello接口返回404错误求助
部署在Azure App Service的React+Vite+Express(Node.js)应用/hello接口返回404错误求助
我把一个简单的Web应用部署到Azure App Service之后,根路径访问完全正常,但一访问baseurl/hello这个接口就跳404,错误信息如下:
Error: 404 Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
本地运行的时候完全没问题,我都快想破头了,不知道怎么让Express在Azure上正确处理请求,求各位大神帮忙看看!
当前使用的配置文件
web.config
<configuration> <system.webServer> <rewrite> <rules> <!-- SPA fallback rule to handle client-side routing --> <rule name="SPA Routing" stopProcessing="true"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <!-- Exclude /hello and any other endpoints --> <add input="{REQUEST_URI}" pattern="^/hello" negate="true" /> </conditions> <action type="Rewrite" url="/index.html" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
package.json(仅scripts部分)
"scripts": { "dev": "nodemon -w src/server -x tsx src/server/main.ts", "start": "cross-env NODE_ENV=production tsx src/server/main.ts", "build": "vite build && cpx web.config dist/"
server/main.ts
import express from "express"; import ViteExpress from "vite-express"; const app = express(); app.get("/hello", (_, res) => { res.send("Hello Vite + React + TypeScript!"); }); const port = process.env.PORT || 3000; ViteExpress.listen(app, +port, () => console.log(`Server is listening on port ${port}...`) );
项目与部署情况
- 本地开发环境中项目结构完整,包含构建后的
dist目录 - 已通过Azure的Kudu工具确认,App Service的
wwwroot目录下的部署文件全部到位
备注:内容来源于stack exchange,提问作者aamer saleem
相关产品推荐
相关产品推荐

