Skip to content

Instantly share code, notes, and snippets.

@zazk
Created February 25, 2025 21:43
Show Gist options
  • Save zazk/9b2a575da627270bd5073a21e5a57869 to your computer and use it in GitHub Desktop.
Save zazk/9b2a575da627270bd5073a21e5a57869 to your computer and use it in GitHub Desktop.
Deploy API to Netlify

Deploying a Node.js/Express application to Netlify requires some adjustments because Netlify is primarily designed for static sites and serverless functions. However, you can deploy your Node.js/Express app to Netlify using Netlify Functions or by combining it with a service like Heroku or Render for backend hosting. Below, I'll guide you through deploying your app using Netlify Functions.


Steps to Deploy to Netlify

1. Refactor Your App for Netlify Functions

Netlify Functions allow you to run server-side code as serverless functions. You’ll need to refactor your Express app into a function.

Update your index.js to work as a Netlify Function:

// netlify/functions/fetchTable.js
const axios = require('axios');
const cheerio = require('cheerio');

exports.handler = async (event, context) => {
    try {
        const url = 'https://documents.bvl.com.pe/empresas/entrder1.htm';
        const { data } = await axios.get(url);
        const $ = cheerio.load(data);

        const tableData = [];

        // Select the table and iterate over rows
        $('table.Tablas').eq(1).find('tbody tr').each((i, row) => {
            const columns = $(row).find('td');
            if (columns.length > 0) {
                const rowData = {
                    valor: $(columns[0]).text().trim(),
                    derechos: $(columns[1]).text().trim(),
                    conceptoEjercicio: $(columns[2]).text().trim(),
                    fechaAcuerdo: $(columns[3]).text().trim(),
                    fechaCorte: $(columns[4]).text().trim(),
                    fechaRegistro: $(columns[5]).text().trim(),
                    fechaEntrega: $(columns[6]).text().trim(),
                };
                tableData.push(rowData);
            }
        });

        return {
            statusCode: 200,
            body: JSON.stringify(tableData),
        };
    } catch (error) {
        return {
            statusCode: 500,
            body: JSON.stringify({ error: 'An error occurred while fetching the data' }),
        };
    }
};

2. Set Up Your Project Structure

Create the following structure in your project:

bvl-api/
├── netlify/
│   └── functions/
│       └── fetchTable.js
├── package.json
├── netlify.toml
└── .gitignore

3. Create a netlify.toml File

This file tells Netlify how to build and deploy your app.

[build]
  functions = "netlify/functions"

4. Update package.json

Ensure your package.json includes the necessary dependencies and scripts:

{
  "name": "bvl-api",
  "version": "1.0.0",
  "scripts": {
    "build": "echo 'Building...'",
    "start": "netlify dev"
  },
  "dependencies": {
    "axios": "^1.6.2",
    "cheerio": "^1.0.0-rc.12"
  },
  "devDependencies": {
    "netlify-cli": "^16.0.0"
  }
}

5. Install Dependencies Locally

Run the following command to install dependencies:

npm install

6. Test Locally with Netlify Dev

Netlify provides a local development server. Run the following command to test your function locally:

npx netlify dev

Visit http://localhost:8888/.netlify/functions/fetchTable to see your function in action.


7. Deploy to Netlify

  1. Push Your Code to GitHub:

    • Create a GitHub repository and push your code to it.
  2. Connect to Netlify:

    • Go to Netlify and log in.
    • Click on Add new site > Import an existing project.
    • Connect your GitHub repository.
  3. Configure Build Settings:

    • Set the build command to npm run build.
    • Set the publish directory to ./ (or leave it blank if you don’t have a static site).
  4. Deploy:

    • Netlify will automatically detect your netlify.toml file and deploy your site.

8. Access Your Function

Once deployed, your function will be available at:

https://<your-site-name>.netlify.app/.netlify/functions/fetchTable

Alternative: Deploy Backend to Render or Heroku

If you prefer to keep your Express app as a full backend (not serverless), you can deploy it to Render or Heroku and use Netlify for the frontend.

  1. Deploy Backend to Render/Heroku:

  2. Use Netlify for Frontend:

    • Deploy your frontend to Netlify and make API calls to your Render/Heroku backend.

Summary

  • Use Netlify Functions to deploy your Node.js/Express app as a serverless function.
  • Alternatively, deploy the backend to Render or Heroku and use Netlify for the frontend.
  • Test locally with netlify dev before deploying.

Let me know if you need further assistance! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment