Netlify from GitHub Repo, using vite-express (szymmis/vite-express)

Hi there,

Im quite new to netlify, and got a problem with deploying a project.

I’ve came across a great pre-setup of vite with express, and got it connected to mongodb atlas.

My problem is, I’m not sure how to integrate that with netflify’s framework.

The vite-express is from a github repo, it basically integrates a project so that it runs an express server and the vite frontend simultaneously.

I got the setup to work on local environment wih your cli (netlify-cli, latest version), using env tables, and simply by

  • copying the code I’ve had in the vite-express’s src/server/main.ts to netlify/functions/api.ts
  • changing the package.json to run following:
"scripts": {
    "dev": "nodemon netlify/functions/api.ts -w netlify/functions",
    "start": "SET NODE_ENV=production & ts-node netlify/functions/api.ts",
    "build": "vite build"
  },

The problem is when the netlify online deployment starts and finishes, as far as I can tell from the console and Network tab, whenever I try to make a request to the express api framework, it shows its not connected to the mongodb, and seems like the express env stopped or didnt even start due to an error.

Could you tell me either how to set this up correctly, or how to test it on local environment with the same build? Again, in local env, using the netlify dev command, everything works correctly.

@LarryTest21 As you’ve found, there’s no runtime node.js hosting on Netlify.

See:

I’m not trying to deploy any server. I’m using the netlify functions to build an express “server”, which is just a middleware inbetween MongoDB atlas and the Vite frontend.

My problem again is simply, that when I build and deploy on netlify, the express “server as a function” either doesnt seem to be running or it crashes, but since there is no real console log for within the netlify environment, I can’t figure out exactly what is the problem.

I’m almost sure I’m not correctly setting up the netlify.toml settings for build and run, but locally, everything does work fine. Meaning, building my project with the netlify/functions/api.ts (according to the helper here btw - Express on Netlify | Netlify Docs) and using this

"scripts": {
    "dev": "nodemon netlify/functions/api.ts -w netlify/functions",
    "start": "SET NODE_ENV=production & ts-node netlify/functions/api.ts",
    "build": "vite build"
  },

in my top directory package.json.

Also, for some reason, even though I have the “esbuild” in this package.json, when I try to deploy, first it gave me an error about esbuild, that it should be “externally imported”, and that it cant find a pkg for “lightnincss” - the latter I dont even use anywhere in my project btw, and it is in the package.json.

So my netlify.toml looks like this (prob some unnecessary lines there, but I’m currently in trial and error mode :smiley: )

[dev]
command = "npm run build"
targetPort = 5173
port= 3333
publish = "dist"

[build]
targetPort = 5173
[start]
command="nodemon netlify/functions/api.ts -w netlify/functions"
targetPort = 5173
[functions]
  external_node_modules = ["express", "esbuild", "lightningcss"]
  node_bundler = "esbuild"
[[redirects]]
  force = true
  from = "/api/*"
  to = "/.netlify/functions/api/:splat"

The part I’m concerned about is this:

Is the code actually formatted as a function, or is it just whatever code was in the file moved into the /functions/ folder?

You probably have already, but if you haven’t, you might want to read the documentation for Netlify Functions:

There’s also a new version too which is mentioned at the top of the docs and has some info on the blog:

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.

@LarryTest21 I’m unfamiliar with the Express specific integration referenced at Express on Netlify | Netlify Docs

I tend to only help with base level functionality, not anything that’s framework specific or relies on middleware.

Best of luck getting it sorted.