How to Deploy Nodejs and Express Backend service on Netlify

Hosting Nodejs and Express backend api service on netlify is completely free and easy to use


step 1 : Create folder netlifyHosting

              mkdir netlifyHosting
              cd netlifyHosting

Step 2 : Create Node project using 

      npm init -y

step 3 : Install express 

      npm install express --save   

step 4 : Install serverless-http

              npm install serverless-http --save 

step 5 : Install netlify-lambda

       npm install netlify-lambda --save

step 6 : Create file netlify.toml and create below code in it


             [build]
     functions="functions"




step 7 : Edit package.json file 

      a) "main": "./src/api.js"
              b) "scripts": {
               "start": "NODE_ENV=development ./node_modules/.bin/netlify-lambda serve src",
               "build": "NODE_ENV=production ./node_modules/.bin/netlify-lambda build src"
              }




step 8 : Create src folder in root directory

step 9 : Create functions folder in root directory      

step 10 : Create api.js file inside src folder

step 11 : Write below code in api.js

               const express = require("express");
               const serverless = require("serverless-http");
        
               const app = express();
               const router = express.Router();
 
                router.get("/", (req, res) => {  
            res.json({
            hello: "hi!",
                      });
                 });

                app.use(`/.netlify/functions/api`, router);

               module.exports = app;
               module.exports.handler = serverless(app);





step 12 : npm start



step 13 : open chrome browser and entry

         http://localhost:9000/.netlify/functions/api




step 14 : Now Deploy your application on netlify its ready to deploy


for complete Details

 directory structure should look like


    - node_module
    - functions
    - netlify.toml
    - src
       - api.js
    - package.json



 

Comments

  1. Awesome it's very easy to understand. Amazing 👍 .. Well done

    ReplyDelete
  2. Very neat steps. No extra theory, that saves confusion.

    ReplyDelete
  3. Wow... Its quite simple and easy to understand

    ReplyDelete
  4. Kadhi pasn me sopi padhat sodhat hoto aaj sapdli Dhanyawad 🙏 ati uttam

    ReplyDelete
  5. Great job 🤘Keep it up...💯
    All The Best... 👍

    ReplyDelete
  6. After step: step 12 : npm start
    'NODE_ENV' is not recognized as an internal or external command,
    operable program or batch file.

    ReplyDelete

Post a Comment