【问题标题】:Next Js api static export return 405 in iisNext Js api静态导出在iis中返回405
【发布时间】:2021-02-26 17:34:12
【问题描述】:

当我使用 next export 导出 NextJs 应用程序并在 iis (Windows Server 2019) 中发布时,应用 url 重写有助于每个路由可达,但只有 api 部分不可达。当我想去 api 路由时,它返回 http 405 错误。我尝试在此处附上 api 代码和项目设置, index.js

import Head from 'next/head'

export default function Home() {
return (
<div className="container">
  <Head>
    <title>Create Next App</title>
    <link rel="icon" href="/favicon.ico" />
  </Head>

  <main>
    <h1 className="title">
      Welcome to <a href="https://nextjs.org">Next.js!</a>
    </h1>

    <p className="description">
      Get started by editing <code>pages/index.js</code>
    </p>

    <div className="grid">
      <a href="https://nextjs.org/docs" className="card">
        <h3>Documentation &rarr;</h3>
        <p>Find in-depth information about Next.js features and API.</p>
      </a>

      <a href="https://nextjs.org/learn" className="card">
        <h3>Learn &rarr;</h3>
        <p>Learn about Next.js in an interactive course with quizzes!</p>
      </a>

      <a
        href="https://github.com/vercel/next.js/tree/master/examples"
        className="card"
      >
        <h3>Examples &rarr;</h3>
        <p>Discover and deploy boilerplate example Next.js projects.</p>
      </a>

      <a
        href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
        className="card"
      >
        <h3>Deploy &rarr;</h3>
        <p>
          Instantly deploy your Next.js site to a public URL with Vercel.
        </p>
      </a>
    </div>
  </main>

  <footer>
    <a
      href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
      target="_blank"
      rel="noopener noreferrer"
    >
      Powered by{' '}
      <img src="/vercel.svg" alt="Vercel Logo" className="logo" />
    </a>
  </footer>

  <style jsx>{`
    .container {
      min-height: 100vh;
      padding: 0 0.5rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    main {
      padding: 5rem 0;
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    footer {
      width: 100%;
      height: 100px;
      border-top: 1px solid #eaeaea;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    footer img {
      margin-left: 0.5rem;
    }

    footer a {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    a {
      color: inherit;
      text-decoration: none;
    }

    .title a {
      color: #0070f3;
      text-decoration: none;
    }

    .title a:hover,
    .title a:focus,
    .title a:active {
      text-decoration: underline;
    }

    .title {
      margin: 0;
      line-height: 1.15;
      font-size: 4rem;
    }

    .title,
    .description {
      text-align: center;
    }

    .description {
      line-height: 1.5;
      font-size: 1.5rem;
    }

    code {
      background: #fafafa;
      border-radius: 5px;
      padding: 0.75rem;
      font-size: 1.1rem;
      font-family: Menlo, Monaco, Lucida Console, Liberation Mono,
        DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
    }

    .grid {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-wrap: wrap;

      max-width: 800px;
      margin-top: 3rem;
    }

    .card {
      margin: 1rem;
      flex-basis: 45%;
      padding: 1.5rem;
      text-align: left;
      color: inherit;
      text-decoration: none;
      border: 1px solid #eaeaea;
      border-radius: 10px;
      transition: color 0.15s ease, border-color 0.15s ease;
    }

    .card:hover,
    .card:focus,
    .card:active {
      color: #0070f3;
      border-color: #0070f3;
    }

    .card h3 {
      margin: 0 0 1rem 0;
      font-size: 1.5rem;
    }

    .card p {
      margin: 0;
      font-size: 1.25rem;
      line-height: 1.5;
    }

    .logo {
      height: 1em;
    }

    @media (max-width: 600px) {
      .grid {
        width: 100%;
        flex-direction: column;
      }
    }
  `}</style>

  <style jsx global>{`
    html,
    body {
      padding: 0;
      margin: 0;
      font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
        Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
        sans-serif;
    }

    * {
      box-sizing: border-box;
    }
  `}</style>
</div>
)
}

你好.js

import { NextApiRequest, NextApiResponse } from "next";

export default async (req, res) => {
if (req.method === 'POST') {
  console.log(req)
  console.log(req.body) 
} else {
  // Handle any other HTTP method
}
res.statusCode = 200
res.json({ trx_id: req.body.transaction_code })
}

package.json

{
"name": "learn-starter",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"export":"next export"
},
"dependencies": {
"next": "^10.0.0",
"react": "16.13.1",
"react-dom": "16.13.1",
"urlencoded-body-parser": "^3.0.0"
 }
 }

当通过 fiddler 请求时,返回以下响应,

谁能帮我解决这个问题?

** 更新:到目前为止,我无法使用静态导出来做到这一点。如果在静态文件中不可能,那么我会期待。

** 最终更新:所以经过几天的搜索和尝试,我得出的结论是 nextjs 静态导出无法帮助我实现这个输出。我求助于另一个 dot net mvc 应用程序,该应用程序接受发布的请求,处理数据,然后通过 GET 请求重定向到我的 nextjs 应用程序。由于我们使用 JWT 令牌,因此这里的身份验证没有问题。感谢大家参与这个话题。

【问题讨论】:

    标签: reactjs iis next.js http-status-code-405


    【解决方案1】:

    当请求的处理程序未配置为禁止此 POST 动词时,您会收到此 405 method not allowed 错误。

    您可以按照以下步骤解决问题。

    1)打开iis manager,选择你的站点。

    2)双击处理程序映射。

    3) 选择 StaticFile handler 从那里点击“Request Restrictions”,然后点击“Verbs”

    4)确保文本区域中存在适当的动词或选择所有动词。(在您的情况下,如果它不可用,您需要添加 POST 动词)

    对 ExtensionlessUrlHandler-ISAPI-4.0 /_32bit /_64bit 处理程序执行相同的操作。

    如果您仍然遇到问题,可以尝试删除 WebDAV Publishing iis 功能。

    【讨论】:

    • 我已经按照你说的做了。根本没有安装 Webdev 发布。但是 405 问题仍然存在。
    • @Ayan_84 API 路由不受此方法支持,因为它们无法预呈现为 HTML。 API Routes 不能与 next export 一起使用,您可以参考此链接了解更多详细信息:link1,link2
    • @Ayan_84 如果您觉得回复有帮助,我请求您将建议标记为答案。这将帮助面临同样问题的其他人。感谢您的理解。
    • 它有用但不是实际的解决方案,所以我不应该将其标记为已接受的答案。相反,它引导我找到解决问题的其他解决方案。
    猜你喜欢
    • 2012-08-29
    • 2019-07-26
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2019-03-29
    • 2021-10-23
    • 2021-09-17
    • 2020-11-30
    相关资源
    最近更新 更多