node.js - 无法在 Azure 应用服务上运行 Next.js 应用
问题描述
我正在尝试让最基本的 Next.js 应用在 Azure 应用服务中运行(URL 是https://asmkp-rich-test2.azurewebsites.net/)。
我可以让一个基本的 Node 应用程序运行良好,从 git 部署(存储库在这里:https ://github.com/Richiban/nextjs-azure-test ,分支是release
)。
但是,我所做的任何事情都不会让这个应用程序运行。
如果您点击该 URL,您将看到:
The page cannot be displayed because an internal server error has occurred.
std 输出中没有任何内容,除了:
Node version: v10.6.0
Done
[5:45:37 PM] Compiling server
[5:45:38 PM] Compiling client
[5:45:38 PM] Compiled server in 1000ms
[5:45:41 PM] Compiled client in 3s
DONE Compiled successfully in 3859ms5:45:41 PM
App prepared
Got handle
Ready on http://localhost:8080
除了以下内容之外,错误输出中没有任何内容:
(node:22980) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
你可以在 git repo 中看到我server.js
的,但我也会在此处包含它以方便阅读:
const http = require("http");
const next = require("next");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
console.log("Node version: " + process.version);
app.prepare()
.then(() => {
console.log("App prepared");
const handle = app.getRequestHandler();
console.log("Got handle");
http.createServer(function(req, res) {
console.log(`Processing incoming request`);
try {
handle(req, res).catch(function(e) {
console.log(`Error caught3: ` + e);
console.log(e);
});
console.log(`Incoming request done`);
} catch (e) {
console.log(`Error caught: ` + e);
console.log(e);
}
}).listen(port);
console.log(`> Ready on http://localhost:${port}`);
})
.catch(function(e) {
console.log(`Error caught2: ` + e);
console.log(e);
});
console.log("Done");
正如您可能从我塞在那里的大量日志中看到的那样,我今天对此束手无策。
所以,总而言之,我有一个最简单的 Next.js 应用程序,我正在使用 Git 将它部署到 Azure 应用程序服务,虽然它在我的机器上运行得很好,但在 Azure 中我收到一条看起来像 no 的毫无意义的错误消息更多细节。
请帮忙。
解决方案
DeprecationWarning 是一个红鲱鱼。您看到此一般错误是因为 iisnode 无法与节点进程通信。
process.env.PORT 在使用 iisnode 时实际上是一个管道名称,因此 parseInt 失败并使用您的后备端口 3000。这意味着您的 next.js 应用程序正在侦听错误的端口。相应地更新您设置端口号的行:
const port = process.env.PORT;
推荐阅读
- apache-kafka - 在 SessionWindows 启动和结束时向 Kafka 发送消息
- react-router-v4 - 对于多个可选参数,React 路由器在 // 之后忽略 URL
- android - 与 Kotlin 中的泛型类型配对
- java - Spring Boot 未验证实体上的嵌入式对象
- angular - 如何在 angular apollo 客户端中停止轮询?
- asp.net - 将多个 301 重定向合并为一个用于 ASP.NET URL 重写模块
- typescript - VSCode Typescript Intellisense 在最简单的项目设置中不起作用
- javascript - CKEDITOR 使用 DecoupledEditor 看不到内容区域
- c++ - clang++ 编译时输出信息错误
- python - 如何让正则表达式在 B 的最大匹配次数后停止搜索 A