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.
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' }),
};
}
};
Create the following structure in your project:
bvl-api/
├── netlify/
│ └── functions/
│ └── fetchTable.js
├── package.json
├── netlify.toml
└── .gitignore
This file tells Netlify how to build and deploy your app.
[build]
functions = "netlify/functions"
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"
}
}
Run the following command to install dependencies:
npm install
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.
-
Push Your Code to GitHub:
- Create a GitHub repository and push your code to it.
-
Connect to Netlify:
- Go to Netlify and log in.
- Click on Add new site > Import an existing project.
- Connect your GitHub repository.
-
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).
- Set the build command to
-
Deploy:
- Netlify will automatically detect your
netlify.toml
file and deploy your site.
- Netlify will automatically detect your
Once deployed, your function will be available at:
https://<your-site-name>.netlify.app/.netlify/functions/fetchTable
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.
-
Deploy Backend to Render/Heroku:
-
Use Netlify for Frontend:
- Deploy your frontend to Netlify and make API calls to your Render/Heroku backend.
- 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! 🚀