ExpressJS on Netlify

Limitation

Approach use default location

Mean the functions folder placed at root location

  • netlify.toml
[build]
command = "npm install && npm run build"
functions = "functions"
  • package.json
{
"name": "expressjs_netlify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "netlify-lambda build express",
"start": "netlify-lambda serve express"
},
"devDependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"netlify-lambda": "^1.6.3",
"serverless-http": "^2.3.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {}
}

On https://app.netlify.com/ Build settings, make sure the Pulish directory is Not set

Try to test and it works

Approach use dist location

Some SPA build to dist folder, and you want to integrate functions lambda too

  • netlify.toml
[build]
command = "npm install && npm run build"
functions = "dist/functions"
  • package.json
{
"name": "expressjs_netlify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rm -rf dist && mkdir -p dist && netlify-lambda build express",
"start": "netlify-lambda serve express"
},
"devDependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"netlify-lambda": "^1.6.3",
"serverless-http": "^2.3.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {}
}

On https://app.netlify.com/ Build settings, make sure the Pulish directory is dist

Example repo

https://github.com/nhancv/expressjs_netlify

Leave a Reply

Your email address will not be published.Required fields are marked *