skip to Main Content

Im trying to deploy my website on cpanel. Im using node.js application with cpanel. see config:

enter image description here

but then i start my server it says "503 service unavailable".

enter image description here

when i try to edit my server.js file i see these weird errors.

enter image description here
enter image description here
enter image description here

server.js

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

const dev = process.env.NODE_ENV !== "production";
const hostname = process.env.HOSTNAME || "localhost";
const port = process.env.PORT || 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer(async function (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;
      await handle(req, res, parsedUrl);
    } catch (err) {
      console.error("Error occurred handling", req.url, err);
      res.statusCode = 500;
      res.end("internal server error");
    }
  }).listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://${hostname}:${port}`);
  });
});

2

Answers


  1. this might be better served as a comment and I apologise if it is but I’m unable to comment. I do however have something to contribute that may help move us towards an answer for this one.

    I’m having a similar issue with my own application, what I have discovered is that however it’s being interpreted by the server infrastructure what it wants is a semicolon after ‘async’. This makes me wonder if perhaps in this kind of arena ‘async’ does something different to what we, the OP and I, are expecting it to, and how it typically operates on our local machines.

    To Clarify, this block yields the same warning errors as OP mentions:

    const register = async (req,res) => {
      const { username, email , password: Npassword, privilege} = req.body;
      if (!email || !Npassword || !username || !privilege)
        return res.json({ status: "error", error:"All Fields Required."});
      else {

    However when the ‘;’ is added after async those warning go away, it isn’t a solution though as adding the semicolon there causes other warnings to pop up for the rest of the code:

    const register = async; (req,res) => {
      const { username, email , password: Npassword, privilege} = req.body;
      if (!email || !Npassword || !username || !privilege) 
        return res.json({ status: "error", error:"All Fields Required."});
      else {

    This is as much as I’ve been able to work out so far. As with OP my application runs perfectly fine on local machine and only has an issue when trying to deploy it on cpanel.

    Login or Signup to reply.
  2. If your nextjs project is working in development mode but not in production mode that’s because you did not do :

    npm run build

    before deploying your code.

    The server.js should looks like this :

    const { createServer } = require("http");
    const { parse } = require("url");
    const next = require("next");
    
    const dev = process.env.NODE_ENV !== "production";
    const hostname = "yourdomain.com";
    const port = process.env.PORT;
    // when using middleware `hostname` and `port` must be provided below
    const app = next({ dev, hostname, port });
    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}`);
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search