【问题标题】:Express.js POST returns 404 on IISExpress.js POST 在 IIS 上返回 404
【发布时间】:2019-06-08 00:32:09
【问题描述】:

我有一个使用 IIS 托管的快速服务器。我遇到了一个问题,我的 POST 请求都返回 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 和 GET http://example.com/test 这样的 REST 客户端,它可以正常工作,但 POST 到 http://example.com/create-pdf 会返回 404。我在这里遗漏了什么吗?

【问题讨论】:

  • 忽略。读错问题。
  • @Richard 不幸的是同样的问题。
  • 在节点脚本中添加create-pdf的get方法有效吗?
  • @Richard 是的,如果我添加 app.get('/create-pdf', function (req, res) { res.send('Hello Dev!');});它有效。
  • @Richard 如果我添加一个简单的 POST,例如: app.post('/beep', function(res, req{res.send('received')}); 我收到 404

标签: node.js express iis puppeteer


【解决方案1】:

重写/重定向的默认请求方法始终是 GET。对于其他动词重写,它们需要显式添加。

添加一条与您的规则完全相同的新规则,并将以下条件添加到规则中:

<conditions>
  <add input="{REQUEST_METHOD}" pattern="^POST$" />
</conditions>

【讨论】:

  • 我似乎仍然收到 404。
  • 看来我的订购有误,这行得通。谢谢
  • 我假设您以不同的方式命名新规则?
猜你喜欢
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多