skip to Main Content

I have a problem in routing API in my NodeJS application. which is fully working on localhost but not working on cPanel NodeJS server.

when I call localhost API. it’s properly working.

http://localhost:5000/api/admin/login

but
when I calling API after deploy application on server. it’s not working

https://example.com/backend/api/admin/login

it’s giving the error which it is ==>

Cannot POST /backend/api/admin/login

this is my app.js file

require("dotenv").config();

const express = require("express")
const bodyParser = require("body-parser")
var cors = require('cors')
const app = express();
const path = require("path")
const userRouter = require('./api/admin/admin.router')
const gstRegistrationRouter = require('./api/frontApp/gstRegistration/gstRegistration.router')
const userProfileRouter = require('./api/frontApp/userProfile/userProfile.router')

app.use(express.static(path.join(__dirname+'./Images')));
app.use('/images',express.static('Images'));

const PORT = process.env.port || 5000;

app.use(cors({
    origin:["https://example.com/"],
    methods:["GET", "POST", "PATCH", "DELETE"],
    credentials:true
}))
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}))

app.use("/api/admin",userRouter)
app.use("/api/gstRegistration",gstRegistrationRouter)
app.use("/api/userProfile",userProfileRouter)


app.listen(PORT, () =>{
    console.log("Server up and running on PORT ." ,PORT);
})

this is my admin.router file

const { getUsers, createuser, getUserByUserId, login, updateUser,forgotPasswordPost,resetPasswordPost,uploadImage} = require("./admin.controller")
const { checkToken } = require("../../auth/token_validation")
const router = require("express").Router();
var multer = require('multer'); 
var path   = require('path');

var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './Images')
    },
    filename: function(req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
    }
})

router.get("/", getUsers);
router.get("/:id",getUserByUserId);
router.post("/", createuser);
router.patch("/", updateUser);

router.post("/login", login);


router.post("/forgot-password",forgotPasswordPost);
router.post("/upload",multer({storage:storage}).single('file'),uploadImage);

router.post('/reset-password/:id/:token',resetPasswordPost);


module.exports = router

3

Answers


  1. Which one have you deployed, front end or backend? Or both? And how are you calling your API from the front end?

    Login or Signup to reply.
  2. I think you are calling the wrong API in localhost you are calling http://localhost:5000/api/admin/login.
    But on the server, you are calling.
    https://example.com/backend/api/admin/login
    in Server /backend is extra.
    try calling https://example.com/api/admin/login

    Login or Signup to reply.
  3. Cannot POST /backend/api/admin/login
    

    The nodejs server is being hit with that URL. You don’t have a route for that URL, hence the error.

    You have several options:

    • Configure the proxy server to rewrite the URL before sending it to the nodejs server. For instance, with nginx you would be able to add something like rewrite ^/backend(.*)$ $1 last; to the configuration. I have never used cPanel, so I don’t know if that’s possible.
    • Add the full path to the express routes, the prefix could be configured with an environment variable if needed.
    • Change your domain to backend.example.com, so that your requests would be backend.example.com/api/admin/login and the nodejs server would get the right path.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search