<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sakis Pal</title>
    <description>The latest articles on DEV Community by Sakis Pal (@sakispal).</description>
    <link>https://dev.to/sakispal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2393988%2Fcc713332-c12a-45f3-b8a8-79912c6608ea.png</url>
      <title>DEV Community: Sakis Pal</title>
      <link>https://dev.to/sakispal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sakispal"/>
    <language>en</language>
    <item>
      <title>How to deploy a Node.js Express app on Netlify (2024)</title>
      <dc:creator>Sakis Pal</dc:creator>
      <pubDate>Sat, 09 Nov 2024 21:15:59 +0000</pubDate>
      <link>https://dev.to/sakispal/how-to-deploy-a-nodejs-express-app-on-netlify-2024-1hci</link>
      <guid>https://dev.to/sakispal/how-to-deploy-a-nodejs-express-app-on-netlify-2024-1hci</guid>
      <description>&lt;p&gt;Netlify does not make it easy to host an Express web app. I hope you will find this guide useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Structure your app in a similar way as the below: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdiklrkpggvaagobp28kh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdiklrkpggvaagobp28kh.PNG" alt="Image description" width="512" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow this guide &lt;a href="https://docs.netlify.com/frameworks/express/" rel="noopener noreferrer"&gt;https://docs.netlify.com/frameworks/express/&lt;/a&gt; . I deleted node_bundler = "esbuild" because it threw an ESM error and everything worked fine regardless. &lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbao64no7fs3ew5uo24v.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbao64no7fs3ew5uo24v.PNG" alt="Image description" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(Optional) Use a local server file for development to keep things more tidy such as the image:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7f7imypep03a67nccbm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7f7imypep03a67nccbm.PNG" alt="Image description" width="651" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The package.json can look like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "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",
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write your server.mjs code such as the image:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkjsb7zv87u7cx6yq9mt.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkjsb7zv87u7cx6yq9mt.PNG" alt="Image description" width="800" height="859"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="https://my-app.netlify.app/css/styles.css"&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run

&lt;code&gt;netlify dev&lt;/code&gt;

on the console to test before deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key lessons
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;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
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[functions]
  included_files = [
    "static/views/**"  # Include all files in static/views for server-side access
  ] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Static assets are better served by Netlify's CDN, which will happen automatically if in the netlify.toml file you use the command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[build]
  publish = "static"  # Static assets to be served by Netlify's CDN. Folder defaults to public
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Console.log statements outside of router endpoints are not shown on the console.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>express</category>
      <category>netlify</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
