Thanks for the replies, but if you read carefully my comment to which you replied, I myself included the link as to the article on Netlify, how to setup the express server as a function.
I’m currently testing all of my project locally, with the netlify cli (netlify dev, netlify serve, netlify.toml etc)
Currently after many iterations of possibilites, these are the basic and fundamentals:
Previously, with that github repo I posted in my 1st comment, which is a premade vite-express setup, it was functioning as first deploying the express server, which then opened up on another port for the vite static site with the following command in the express main.ts:
ViteExpress.listen(app, 5173, () =>
console.log("Server is listening on http://localhost:5173"))
This is a premade script, as a node module also, made by the guy who made the repo. This one I left out from the netlify version
Yes, otherwise, the code was the same for the newly created .netlify/functions/api.ts
import express, { Router } from "express";
import serverless from "serverless-http";
import AuthRoutes from "../../src/server/routes/authRoutes"
import bodyParser from "body-parser"
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser')
const mongoURI = process.env.MONGO_URI;
const database = mongoose.connection;
database.on('error', (error: any) => {
console.log(error)
})
database.once('connected', () => {
console.log('Database Connected');
})
const app = express();
mongoose.connect(mongoURI)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '10mb' }));
app.use(cookieParser())
app.use('/api', AuthRoutes)
export const handler = serverless(app);
I created a netlify.toml file, which looks like this
[dev]
command = "npm run dev"
targetPort = 5173 # The port for your application server, framework or site generator
port = 8888 # The port that the netlify dev will be accessible on
[functions]
external_node_modules = ["express", "esbuild", "lightningcss"]
node_bundler = "esbuild"
[[redirects]]
force = true
from = "/api/*"
status = 200
to = "/.netlify/functions/api/:splat"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
force = false
First of all, after again, many many iterations of trial and error, I found out that the netlify dev command is probably broken, it cant apply my redirects for the index.html from the netlify.toml file, although yes, it is at the root of my project, same place as the netlify.toml file. I saw this was a problem years ago too, seems like it still is.
Second of all, when I ran the netlify serve command, again, still locally, my project ran smoothly. It correctly, loads the root index.html, runs the express (as a function ofc) server, and it is able to accept the routed mongoDB (which for I use simply the free tier Atlas) requests, without hiccups, delays, crashes or whatever.
I also found out by testing, that when I would change the deploy settings on the website in netlify, eg. Build command, that also changes my deploy settings locally - which is not a problem, i guess the first few seconds when the netlify command ran locally is “hanging”, cause its actually synchronizing some info.
BUT
As soon as I upload the same project (Im using a github repo for this btw, so no user error not uploading certain files or not correctly), and let netlify deploy it, suddenly, none of the requests work.
All the requests, response with status code 200, and in the data of the response, my index.html.
I tried it with “dummy” routes, eg:
router.get("/hello", (req, res) => res.send("Hello World!"));
api.use("/api", router);
and made a simple button for
axios.get("/api/hello").then((res) => {
console.log(`output->res`, res)
The result for this dummy endpoint and request, is the very same. Status code 200, and in the data, my index.html.