How to deploy a Node.js Express app on Netlify (2024)

Netlify does not make it easy to host an Express web app. I hope you will find this guide useful.

Steps

Structure your app in a similar way as the below:

Follow this guide https://docs.netlify.com/frameworks/express/ . I deleted node…


This content originally appeared on DEV Community and was authored by Sakis Pal

Netlify does not make it easy to host an Express web app. I hope you will find this guide useful.

Steps

  • Structure your app in a similar way as the below:

Image description

  • Follow this guide https://docs.netlify.com/frameworks/express/ . I deleted node_bundler = "esbuild" because it threw an ESM error and everything worked fine regardless.
  • When creating the netlify.toml file keep redirects specific to the API and any endpoints you want to serve through Express. A general redirect (i.e. "/*") will mess up your CDN and static file serving.

Image description

  • (Optional) Use a local server file for development to keep things more tidy such as the image:

Image description

  • The package.json can look like this:

     "scripts": {
        "start": "node ./functions/server.js",
        "build": "netlify deploy --prod",
        "build-dev": "NODE_ENV=development webpack --mode development --watch",
        "dev": "NODE_ENV=development node server-local.js",
        "dev-watch": "NODE_ENV=development nodemon --exec node server-local.js",
    },
    
  • Write your server.mjs code such as the image:

Image description

  • Inside the index.html and the rest of the .html files the path to CSS, JS, and other assets is best set to the CDN which will work in both dev and production i.e.:

<link rel="stylesheet" href="https://my-app.netlify.app/css/styles.css">

  • Run netlify dev on the console to test before deployment

Key lessons

  • The server.js or server.mjs app only has access to /netlify/functions. The 'public' or 'dist' or 'static' folder will not be added to the netlify folder unless explicitly specified in the netlify.toml file using the command
[functions]
  included_files = [
    "static/views/**"  # Include all files in static/views for server-side access
  ] 
  • Static assets are better served by Netlify's CDN, which will happen automatically if in the netlify.toml file you use the command
[build]
  publish = "static"  # Static assets to be served by Netlify's CDN. Folder defaults to public
  • Console.log statements outside of router endpoints are not shown on the console.


This content originally appeared on DEV Community and was authored by Sakis Pal


Print Share Comment Cite Upload Translate Updates
APA

Sakis Pal | Sciencx (2024-11-09T21:15:59+00:00) How to deploy a Node.js Express app on Netlify (2024). Retrieved from https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/

MLA
" » How to deploy a Node.js Express app on Netlify (2024)." Sakis Pal | Sciencx - Saturday November 9, 2024, https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/
HARVARD
Sakis Pal | Sciencx Saturday November 9, 2024 » How to deploy a Node.js Express app on Netlify (2024)., viewed ,<https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/>
VANCOUVER
Sakis Pal | Sciencx - » How to deploy a Node.js Express app on Netlify (2024). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/
CHICAGO
" » How to deploy a Node.js Express app on Netlify (2024)." Sakis Pal | Sciencx - Accessed . https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/
IEEE
" » How to deploy a Node.js Express app on Netlify (2024)." Sakis Pal | Sciencx [Online]. Available: https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/. [Accessed: ]
rf:citation
» How to deploy a Node.js Express app on Netlify (2024) | Sakis Pal | Sciencx | https://www.scien.cx/2024/11/09/how-to-deploy-a-node-js-express-app-on-netlify-2024/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.