Unable to deploy Express, EJS site

I’m working on a single-page application that doesn’t require any redirects. I’ve tried running it locally using netlify dev and testing it with Postman, but I’m still encountering issues. The favicon isn’t loading, but I suspect the problem is more significant than that as I also have 404. I believe there’s a configuration error somewhere in my setup, but I’ve tried various combinations without success. Any guidance would be greatly appreciated. :pray:

netlify.toml

[build]
  functions = "functions"
  publish = "public"

[functions.app]
  external_node_modules = ["express", "ejs", "serve-favicon"]
  node_bundler = "esbuild"
  included_files = ["views/**", "public/**"]

[[redirects]]
  from = "/(.*)"
  status = 200
  to = "/.netlify/functions/app/:splat"

packadge.json

{
  "name": "port",
  "version": "1.0.0",
  "description": "webpage",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "build": "netlify-lambda build ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@netlify/functions": "^2.8.2",
    "ejs": "^3.1.10",
    "express": "^4.21.1",
    "netlify-cli": "^17.37.1",
    "netlify-lambda": "^2.0.16",
    "serve-favicon": "^2.5.0",
    "serverless-http": "^3.2.0"
  }
}

app.js

const express = require('express');
const serverless = require('serverless-http');
const app = express();
const path = require("path");
const favicon = require('serve-favicon');

// Set EJS as the template engine
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));

// Serve static files (CSS, JS, etc.)
app.use(express.static(path.join(__dirname, "public")));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))

// Define route to render your main page
app.get("/", (req, res) => {
  res.render("index");
  
});

// Catch-all route to handle all other paths (404 page)
app.get('*', (req, res) => {
  res.status(404).send('Page Not Found');
});

// Define a path for serverless functions
app.use('/.netlify/functions/api', (req, res) => {
  res.send('Serverless Function is working!');
});

// Export serverless handler
module.exports.handler = serverless(app);

@ritualjoint What’s the link to the site?

The only thing that jumps out to me is:

I’m not sure why you’ve said it doesn’t require redirects when it clearly has one.

I don’t believe Netlify’s redirects engine supports regex.

See the format in the documentation for wildcards/splats:

1 Like

Thank you for pointing out the redirects. I took it out, but still doesn`t work. link to site i also added

app.use('/.netlify/functions/app', (req, res) => {
  res.send("Serverless function is active!"); // For testing
});

but comes up: " Error - ENOENT: no such file or directory, stat ‘/public/favicon.ico’ "

packadge_toml_app.zip (1.4 KB)

Apprichiate your help! :pray:

This error:

Seems fairly obviously related to this line:

app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))

It also seems entirely correct, as there is no public/favicon.ico
https://andrasegyed.netlify.app/public/favicon.ico

Secondarily to that though, since you’re in the context of the function at that point, it can’t access files from the CDN, only those bundled with the function.

See included_files:
https://docs.netlify.com/functions/optional-configuration/#bundle-2

1 Like

Thank you. I changed it so many times now trying to trial and error the issue (via tutorials, codium, chatgpt), not realising the path not correct. The path is correct now but page still not found. Let me know if you need more info as I’m lost with this. Thank you!

I tried again with a new barebone repo (not even favicon) and still not working. here is the repo. It was running locally without netlify, but with netlify dev 404 same as deployed, I have no idea where the issue is. :sob:

@ritualjoint I’m not going to spend any time pulling down your repo to run it locally, as your project isn’t constructed how I usually work and doing so would probably just be a waste of my time.

I also don’t host with Netlify myself, so don’t have their CLI installed.

Let me know once you have it hosted on Netlify and a specific path I could check the response of.

1 Like

I understand, appreciate your help @nathanmartin. Here it is https://moonlit-gingersnap-253e40.netlify.app/

@ritualjoint Visiting the root is a 500 error:

Have you checked your Function logs?

If the root isn’t intended to respond successfully, what is a path you would expect to?

now its saying cant find ejs :sweat:
I reinstalled, deleted modules installed dependencies again, I also saw someone on stack suggested to npm i ejs -g but still same, honestly i don’t know at this point. Two deploys with 2 different issues
Oct 25, 11:25:15 AM: 9cc528c3 ERROR Error: Cannot find module ‘ejs’
Error - ENOENT: no such file or directory, stat ‘/public/favicon.ico’

Found a solution all working deployed: https://express-ejs-serverless.netlify.app/

netlify.toml

`[build]
  command = "CI= npm run build"
  functions = "netlify/functions"
  publish = "public"

[functions]
  external_node_modules = ["express", "ejs"]
  node_bundler = "esbuild"
  included_files = ["views/**", "public/**"]

[[redirects]]
  from = "/images/*"
  to = "/images/:splat"
  status = 200

[[redirects]]
  from = "/css/*"
  to = "/css/:splat"
  status = 200

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/server/:splat"
  status = 200`

server.js

const serverless = require("serverless-http");
const app = require("../../app");
console.log("App is starting");

exports.handler = serverless(app);

app.js

const express = require("express");

const path = require("path");

const app = express();

// Set views directory and engine

app.set("view engine", "ejs");

app.set("views", path.resolve(__dirname, "../../views"));

// Static file serving

app.use(express.static(path.join(__dirname, "../../public")));

// Routes

app.get("/", (req, res) => {

res.render("index");

});

module.exports = app;