skip to Main Content

I have taken about 2days to research on this and i have come across several answers but it doesn’t still solve my problem.

I have this api app which i built with nodejs and express js,
when I listen to it on the main port from a port like http://127.0.0.1:8080, it returns the right response of hello world, but when i tried to host it to my cpanel following this steps here How to Host a Node.JS Application With cPanel and visit http:// mysite.com/api same with (https:// mysite.com/api) it returns the error Cannot GET /api/

I don’t know what else to do, i will really appreciate any help.

N/B: my server is running node version 16.13.1 and express version 4.17.2

here is my app.js code

const express = require("express");
const cors = require("cors");


const app = express();
var server  = require('http').createServer(app);
var io      = require('socket.io').listen(server);
var corsOptions = {
  origin: "http://localhost:8080"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

// database
const db = require("./app/models");
const Role = db.role;

db.sequelize.sync();

// simple route
app.post("/", (req, res) => {
  res.json({ message: "Hello World." });
  console.log(res);
});

// routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

Here is my package json code

{
  "name": "node-js-jwt-auth",
  "version": "1.0.0",
  "description": "Node.js Demo for JWT Authentication",
  "main": "app.js",
  "scripts": {
    "test": "set HOST=localhost&& set PORT=8080&& node app.js",
    "dev": "set NODE_ENV=development&& node app.js",
    "prod": "set NODE_ENV=production&& node app.js"
  },
  "keywords": [
    "node.js",
    "jwt",
    "authentication",
    "express",
    "mysql"
  ],
  "author": "bezkoder",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.2",
    "joi": "^17.5.0",
    "jsonwebtoken": "^8.5.1",
    "keygenerator": "^1.0.4",
    "mailgen": "^2.0.16",
    "mysql2": "^2.3.3",
    "nodemailer": "^6.7.2",
    "sequelize": "^5.22.5"
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Alright After much check, I was able to solve my problem.

    The error was from the htaccess file in my root folder of public_html.

    It was resolved automatically after moving the project to a separate folder outside the public_html..

    Thanks Alot for everyone that contributed


  2. Your simple route is a POST request route:

    here:

    // simple route
    app.post("/", (req, res) => {
      res.json({ message: "Hello World." });
      console.log(res);
    });
    

    If you open a given URL in the browser it makes a GET request to your app and the given route. If there is only a POST route it won’t do anything.

    So I think you can just change the simple route to:

    // simple route
    app.get("/", (req, res) => {
      res.json({ message: "Hello World." });
      console.log(res);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search