Express.js POST returns IIS 上的 404
Express.js POST returns 404 on IIS
我有一个使用 IIS 托管的快速服务器。我遇到了一个问题,我的 POST 请求所有 return 404 页面。我的 GET 请求工作正常。
这是我的server.js
const express = require('express');
const cors = require('cors');
const puppeteer = require('puppeteer');
const { createUrl } = require('./urlBuilder');
const app = express();
app.use(express.json());
app.use(cors());
const pdfConfig = {
format: 'A4',
printBackground: true,
margin: {
top: '1cm',
bottom: '1cm',
left: '1.5cm',
right: '1.5cm'
}
};
// Our first route
app.get('/test', function (req, res) {
res.send('Hello Dev!');
});
app.post('/create-pdf', function(req, res) {
const url = createUrl(req.body);
const browser = puppeteer.launch();
browser.then((brw) => {
const page = brw.newPage();
page.then((pg) => {
pg.goto(url).then(() => {
pg.emulateMedia('screen').then(() => {
const buffer = pg.pdf(pdfConfig);
buffer.then((buf) => {
brw.close();
res.end(buf.toString('base64'));
})
})
})
})
});
});
这是我的web.config
<configuration>
<system.webServer>
<!-- indicates that the server.js file is a node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="sendToNode">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
如果我使用 insomnia 之类的 REST 客户端并 GET http://example.com/test it works fine but POST to http://example.com/create-pdf returns 一个 404。我在这里遗漏了什么吗?
rewrite/redirects 的默认请求方法始终是 GET。对于其他动词重写,需要明确添加。
添加一个与现有规则完全相同的新规则,并将以下条件添加到规则中:
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" />
</conditions>
我有一个使用 IIS 托管的快速服务器。我遇到了一个问题,我的 POST 请求所有 return 404 页面。我的 GET 请求工作正常。
这是我的server.js
const express = require('express');
const cors = require('cors');
const puppeteer = require('puppeteer');
const { createUrl } = require('./urlBuilder');
const app = express();
app.use(express.json());
app.use(cors());
const pdfConfig = {
format: 'A4',
printBackground: true,
margin: {
top: '1cm',
bottom: '1cm',
left: '1.5cm',
right: '1.5cm'
}
};
// Our first route
app.get('/test', function (req, res) {
res.send('Hello Dev!');
});
app.post('/create-pdf', function(req, res) {
const url = createUrl(req.body);
const browser = puppeteer.launch();
browser.then((brw) => {
const page = brw.newPage();
page.then((pg) => {
pg.goto(url).then(() => {
pg.emulateMedia('screen').then(() => {
const buffer = pg.pdf(pdfConfig);
buffer.then((buf) => {
brw.close();
res.end(buf.toString('base64'));
})
})
})
})
});
});
这是我的web.config
<configuration>
<system.webServer>
<!-- indicates that the server.js file is a node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="sendToNode">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
如果我使用 insomnia 之类的 REST 客户端并 GET http://example.com/test it works fine but POST to http://example.com/create-pdf returns 一个 404。我在这里遗漏了什么吗?
rewrite/redirects 的默认请求方法始终是 GET。对于其他动词重写,需要明确添加。
添加一个与现有规则完全相同的新规则,并将以下条件添加到规则中:
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" />
</conditions>