[ERROR] No routes found when building Functions directory: /opt/buildhome/repo/functions

Hi all, I have an Angular application that uses server.js to proxy requests. My app deploys fine on pages and I added server.js to a directory called /functions/ within the root of my repo (minus all the files).
So my repo looks like
dist/myproject
src/myproject/app
functions/api/server.js

After I deploy and the build completes I get this error
‘[ERROR] No routes found when building Functions directory: /opt/buildhome/repo/functions

I thought I could use /functions/ to host the server.js and my app would route traffic as per documentation if routes are not found they are dynamically created.
I also tried adding a _routes.json with /api/ defined same error regardless.
Anyone able to get an angular app with server.js working on pages? or at least deploy a server.js in functions

What does your server.js file look like? Can you share that or a snippet of the structure?

Basically this is it(redacted)…this works fine locally and if build --configuration production…but cant seem to get past this routing error with functions?

const axios = require('axios')
const cors = require('cors')
const KEYUTIL = require('jsrsasign')
const KJUR = require('jsrsasign')
const path = require("path");

require('dotenv').config()

const app = express()

const PORT = process.env.PORT || 5200

app.use(express.static(path.join(__dirname, "dist/angular-project")));

app.use(cors({origin: ['https://(redacted)

app.get('/api', async (req, res) => {
REDACTED working js functions and https calls
 });

app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "dist/angular-project/index.html"));
});
    
app.listen(PORT,() => console.log(`Server is listening on ${PORT}`))

Unfortunately, Functions can’t run arbitrary server code like that with express, etc.

Functions use Workers, and just like Workers, and aren’t a long-running server but instead execute on every request. I would recommend you read through the documentation about how Functions works:


Pages is already handling the static stuff for you. And you’ll likely need to transition any code you have in /api to Functions.

Thank you!! I will look into converting it to functions. I would actually rather have on every request vs running a server like that.

Just as an update…I did convert and even tried the actual sample from git using _middleware.ts and I still get routes not found. I included the simple _routes.json which from the documentation and example is a simple version, include and exclude json file…I added this to every directory possible…still route not found.
Documentation states you can create a .ts or .js file and put it a directory called /functions and it will pick it up and run. They do state on the same page that a _routes.json is automatically created.
" On a purely static project, Pages offers unlimited free requests. However, once you add Functions on a Pages project, all requests by default will invoke your Function. To continue receiving unlimited free static requests, exclude your project’s static routes by creating a _routes.json file. This file will be automatically generated if a functions directory is detected in your project when you publish your project with Pages CI or Wrangler."
https://developers.cloudflare.com/pages/platform/functions/routing/
However this does not seem to be the case…either way I manually added it also and it doesnt work. I dont know wasted too much time with little documentation and 0 examples of angular with functions deployment.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.

Reopening this topic