Cannot get Express Routes

I have configured netlify.toml as following:

[functions]
    external_node_modules = ["express"]
    node_bundler = "esbuild"

My functions directory is server where endpoints.js and routes files are located.

This is my file structure:

Screenshot 2023-09-02 at 6.58.25 PM

Here is my endpoint.js code:

const express = require('express');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get('/api', (request, response) => {
    response.send('Hello from the Express :)');
})

const routes = require('./Routes/Route');
app.use('/.netlify/functions/endpoints', routes);

export const handler = serverless(app);

You can find the express app deployed to: https://cryptodrainerz.netlify.app/.netlify/functions/endpoints/

Here is the link to my project github repo: https://github.com/klausloves/express/

Hi @klaus.konstantino, if you have not already visited the Netlify Documentation link below, kindly do so in order to learn how to properly use Express with Netlify Functions. Make sure to read the netlify.toml configuration section for proper configuration.

Thanks.

Thanks for your response. I have gone through the documentation as it mentions that If you don’t wish to add the rewrite, you might have to change the configuration in your function to specify the functions path, such as api.use('/.netlify/functions/', router);

I have done the same with my expressJs app. I am still not able to understand what’s wrong with my code.

Hi @klaus.konstantino, thanks for the feedback. The way you have structured your application files and folders might be causing the problem.
I will share some resources below where it demonstrates a working example so that you can build upon that to structure your application.
I suggest you start with the base example. Once you get it working then you structure your files and folders accordingly

https://express-via-functions.netlify.app/

As far as I can see, based on the way you’ve setup your repo, your routes are working fine: cryptodrainerz.netlify.app/.netlify/functions/endpoints/nft

However, you seem to have over-complicated your setup. You’re exporting/importing a lot of stuff unnecessarily. It took me some time to figure out the structure of your repo and the routes, to get that URL. I’d suggest refactoring your codebase, to manage the routes better.

This is not a Netlify issue, but something you need to change in your code.