Sisky
Sisky

Reputation: 603

Express.js POST returns 404 on IIS

I have an express server that I am hosting using IIS. I'm having an issue where my POST requests all return 404 pages. My GET requests work fine.

Here is my 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'));
          })
        })
      })
    })
  });
});

Here is my 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>

If I use a REST client like insomnia and GET http://example.com/test it works fine but POST to http://example.com/create-pdf returns a 404. Am I missing anything here?

Upvotes: 0

Views: 1412

Answers (1)

Richard
Richard

Reputation: 154

The default request method for rewrite/redirects is always GET. For other verb rewrites they need to be explicitly added.

Add a new rule exactly like the one you have and add the following condition into the rule:

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

Upvotes: 1

Related Questions