NodeJS 如何在IIS上正确运行NextJS(13.4)?

czq61nw1  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(113)

ExpressJS应用程序在IIS上工作正常,但当我试图安装NextJS时,我遇到了这个错误

我遵循了这篇文章的指导方针,但似乎我错过了一些东西。
项目设置

我已经在包含以下代码的根文件夹上创建了app.js作为入口点:

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = process.env.port || 5000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, }) // Here i tried including and removing port and hostname with same result
const handle = app.getRequestHandler()

app.prepare().then(() => {
    createServer(async (req, res) => {
        try {
            // Be sure to pass `true` as the second argument to `url.parse`.
            // This tells it to parse the query portion of the URL.
            const parsedUrl = parse(req.url, true)
            const { pathname, query } = parsedUrl

            if (pathname === '/a') {
                await app.render(req, res, '/a', query)
            } else if (pathname === '/b') {
                await app.render(req, res, '/b', query)
            } else {
                await handle(req, res, parsedUrl)
            }
        } catch (err) {
            console.error('Error occurred handling', req.url, err)
            res.statusCode = 500
            res.end('internal server error')
        }
    })
        .once('error', (err) => {
            console.error(err)
            process.exit(1)
        })
        .listen(port, () => {
            console.log(`> Ready on http://${hostname}:${port}`)
        })
})

然后添加了这个web.config文件:

<configuration>
<system.webServer>
<handlers>
  <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>

<iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />

<rewrite>
  <rules>
    <rule name="nodejs">
      <match url="(.*)" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="/app.js" />
    </rule>
  </rules>
</rewrite> 

<security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="node_modules" />
      <add segment="iisnode" />
    </hiddenSegments>
  </requestFiltering>
</security>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>

并编辑package.json脚本如下:

"scripts": {
"dev": "node app.js",
"build": "next build",
"start": "NODE_ENV=production node app.js",
"lint": "next lint"
},

然后我运行build命令并在IIS上创建一个具有基本设置的站点

当我从visual studio终端运行“npm run dev”时,该应用程序似乎工作正常。但是当我从IIS运行时,我得到404.2错误。请咨询。

pdsfdshx

pdsfdshx1#

我看到你遇到了404.2错误,你可以试试下面的web.config,如果不行,你可以follow the official doc to check the issue
修改你的代码如下:

<configuration>
  <system.webServer>    
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>

    <iisnode node_env="production" nodeProcessCommandLine="&quot;C:\Program Files\nodejs\node.exe&quot;" interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />

  </system.webServer>
    <location path="" overrideMode="Deny">
        <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
        </system.webServer>
    </location>
</configuration>

我已经下载并部署了该项目,它工作正常。

相关问题