通过IIS+iisnode部署Node.js应用时API路由返回404问题
IIS+iisnode部署Express应用时GET路由404问题
在Windows Server上通过IIS结合iisnode部署基于Express的Node.js应用时,出现以下异常:
- 直接用
node命令(如npm start)启动时,所有GET路由均可正常响应; - 通过IIS访问时,主页和Swagger页面加载正常,但所有GET路由均返回404错误。
已配置IIS_IUSRS账户拥有应用文件夹的完全控制权限,怀疑问题出在web.config的URL重写规则上,未正确处理GET路由。相关配置及代码如下:
web.config
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="iisnode" path="server.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true"> <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$"/> </rule> <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> <match url="^server.js\/debug[\/]?"/> </rule> <rule name="StaticContent"> <action type="Rewrite" url="public{REQUEST_URI}"/> </rule> <rule name="DynamicContent"> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> </conditions> <action type="Rewrite" url="server.js"/> </rule> </rules> </rewrite> <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" /> <security> <requestFiltering> <hiddenSegments> <add segment="node_modules" /> </hiddenSegments> <verbs> <add verb="GET" allowed="true" /> <add verb="POST" allowed="true" /> <add verb="PUT" allowed="true" /> <add verb="DELETE" allowed="true" /> </verbs> </requestFiltering> </security> </system.webServer> </configuration>
server.js
// MODULES AND REQUIRES const express = require("express"); const app = express(); const path = require('path'); const swaggerJsDoc = require("swagger-jsdoc"); const swaggerUi = require("swagger-ui-express"); const objectMapper = require('object-mapper'); const cors = require('cors'); // Require Routes var api1 = require('./routes/api1.js') var api2 = require('./routes/api2.js') // PORTS AND BASIC EXPRESS APP SETTINGS const port = process.env.PORT || 3000; // CORS ALLOW ALL. NOTE IP RESTRICTIONS ARE IN PLACE app.use(cors({ origin: '*' })); // ignore request for FavIcon. so there is no error in browser const ignoreFavicon = (req, res, next) => { if (req.originalUrl.includes('favicon.ico')) { res.status(204).end(); } next(); }; // Configure nonFeature app.use(ignoreFavicon); // Root Route - Serve Static File app.get('/', (req, res) => { res.sendFile(path.join(__dirname, '/public/client.html')); }); // SWAGGER UI CONFIGURATION // Primary Swagger Options const options = { customCss: '.swagger-ui .topbar { display: none } .swagger-ui .scheme-container { display: none }' }; // Custom Swagger Options: https://swagger.io/specification/#infoObject const swaggerOptions = { swaggerDefinition: { info: { version: "2.0.0", title: "My App", description: "This page lists the available APIs within my app and allows you to test them.", contact: { name: "My Name" }, servers: [{"url":"http://localhost:3000", "description": "Development server"}] } }, // ['.routes/*.js'] Location for APIs apis: ["./routes/*.js"], }; const swaggerDocs = swaggerJsDoc(swaggerOptions); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs, options)); // ROUTES app.use('/api1', api1) app.use('/api2', api2) // APP LISTEN WITH SSL/HTTPS app.listen(port, () => { console.log(`Server listening on port ${port}`); });
内容的提问来源于stack exchange,提问作者Kode
相关产品推荐
相关产品推荐

