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"
},
然后我运行构建命令并使用基本设置在 IIS 上创建一个站点
当我从 Visual Studio 终端运行“npm run dev”时,该应用程序似乎工作正常。 但是当我从 IIS 运行时出现 404.2 错误。 请指教。
我看到你遇到了404.2错误,你可以尝试下面的web.config,如果不起作用,你可以按照官方文档检查问题。
更改您的代码,如下所示:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="myapp">
<match url="/*" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
<iisnode node_env="production" nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />
</system.webServer>
<location path="" overrideMode="Deny">
<system.webServer>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</location>
</configuration>
我已经下载并部署了该项目,它运行良好。
我遇到了同样的问题,我认为问题出在 iisnode 使用的管道而不是数字端口中。
解决此问题的一个简单解决方案是创建 http 代理服务器和 next.js 服务器。
const { createServer } = require('http');
const { createProxyServer } = require('http-proxy');
const { parse } = require('url');
const next = require('next');
const hostname = 'localhost';
const nextJsPort = 12007;
const startNextJsServer = () => {
// your current code starting next.js server, but using nextJsPort variable as port
};
const startProxyServer = () => {
const proxy = createProxyServer({});
const httpProxyPort = process.env.PORT;
const httpProxyTargetServer = `http://${hostname}:${nextJsPort}`;
createServer((req, res) => {
proxy.web(req, res, { target: httpProxyTargetServer }, err => {
console.error('Error occurred handling redirection', err);
res.statusCode = 500;
res.end('Internal proxy server error');
});
}).listen(httpProxyPort, () => {
console.log(`Proxy server ready on ${httpProxyPort}`);
});
};
startNextJsServer();
startProxyServer();