Richiban
Richiban

Reputation: 5930

Unable to get Next.js app running on Azure App Service

I'm trying to get the most basic Next.js app running in an Azure App Service (the URL is https://asmkp-rich-test2.azurewebsites.net/).

I can get a basic Node app running fine, deploying from git (the repository is here: https://github.com/Richiban/nextjs-azure-test, the branch is release).

However, nothing I do will get this app to run.

If you hit that URL you will see:

The page cannot be displayed because an internal server error has occurred.

There's nothing in the std output except for:

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

and nothing in the error output apart from:

(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.

You can see my server.js in the git repo, but I'll also include it here for easy reading:

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");

As you can probably see from the amount of logging I've stuffed in there I'm at my wits end with this today.

So, in summary, I have the simplest possible Next.js app that I'm deploying with Git to an Azure App Service and, although it runs just fine on my machine, in Azure I get a meaningless error message with what looks like no more detail at all.

Please help.

Upvotes: 1

Views: 2227

Answers (1)

chrisn
chrisn

Reputation: 308

The DeprecationWarning is a red herring. You are seeing this generic error because iisnode is unable to communicate with the node process.

process.env.PORT is actually a pipe name when using iisnode, so is failing the parseInt and using your fallback port of 3000. This means your next.js app is listening on the wrong port. Update the line where you are setting the port number accordingly:

const port = process.env.PORT;

Upvotes: 1

Related Questions