1

I have an app.js node main file where I define my api path as the following

app.get('/api/users', UserController.get);
Below in the same file I have the following

app.use(express.static(path.resolve(__dirname, "./front/build")));
app.get("*", function (request, response) {
  response.sendFile(path.resolve(__dirname, "./front/build", "index.html"));
});

The index.html successfully serves React App.

If I open my heroku app somewhere at my-app.herokuapp.com it will open the React app which is intended but the Problem is my-app.herokuapp.com/api/users also serves index.html file instead of JSON that the endpoint is supposed to return.

I tried
I replaced endpoint route definition to come before the "" definition (didn't suffice)
EVEN more, I removed redirection to index.html but heroku still opens the index.html page with any type of request (the "
" redirection still works). So, it might have cached something?

Is it about cache (how to clean?) or any other suggestions?

levi
  • 3,451
  • 6
  • 50
  • 86

1 Answers1

0

You should create routes and work in a proper flow for each functionality, For Example: //app.js

app.use("/api/user",userRoutes);

//UserRoutes

router.post("/signup",UserController.signup);

//UserController

exports.signup = async (req,res,next) => {
    //Signup function to add a new user  when the user provides required info

}

In this way, you code will be easily accessible and much efficient