xlsx-mongo-api
is a RESTful API built with Node.js and Express that allows you to import and add data from XLSX files to MongoDB. It serves as a convenient interface for utilizing the xlsx-mongo
package.
Start the server by running npm start.
Send a POST request to http://localhost:3000/import with the following parameters:
file: The XLSX file to import.
collectionName (optional): The name of the collection to import/add the data to.
If not provided, it will use the filename as the collection name.
Create a .env
file in the root directory of the project and add the following environment variables:
MONGO_URL=<your-mongodb-connection-url>
MONGO_COLLECTION=<your-mongodb-collection-name>
Example using Axios:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const filePath = path.join('Test.xlsx');
require('dotenv').config();
// Import data from XLSX file to MongoDB
const importFile = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('mongoURL', process.env.MONGO_URL);
formData.append('collectionName', process.env.MONGO_COLLECTION);
const response = await axios.post('http://localhost:3000/import', formData, {
headers: formData.getHeaders(),
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
importFile();
// Add data from XLSX file to existing MongoDB collection
const addData = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('path', filePath);
formData.append('collectionName', 'test2');
formData.append('mongoURL', process.env.MONGO_URL);
const response = await axios.post('http://localhost:3000/add', formData, {
headers: formData.getHeaders(),
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
addData();
Example using RapidAPI:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const filePath = path.join('Test.xlsx');
require('dotenv').config();
// Import data from XLSX file to MongoDB
const importFile = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('mongoURL', process.env.MONGO_URL);
formData.append('collectionName', process.env.MONGO_COLLECTION);
const response = await axios.post('http://localhost:3000/import', formData, {
headers: {
'content-type': `multipart/form-data; boundary=${formData._boundary}`,
'x-rapidapi-key': 'YOUR-RAPIDAPI-KEY',
'x-rapidapi-host': 'xlsx-mongo-api.p.rapidapi.com',
useQueryString: true,
},
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
importFile();
// Add data from XLSX file to existing MongoDB collection
const addData = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('path', filePath);
formData.append('collectionName', 'test2');
formData.append('mongoURL', process.env.MONGO_URL);
const response = await axios.post('http://localhost:3000/add', formData, {
headers: {
'content-type': `multipart/form-data; boundary=${formData._boundary}`,
'x-rapidapi-key': 'YOUR-RAPIDAPI-KEY',
'x-rapidapi-host': 'xlsx-mongo-api.p.rapidapi.com',
useQueryString: true,
},
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
addData();
Check https://github.com/crizmo/xlsx-mongo-api/blob/main/tests/test.js for detailed usage examples.
git clone https://github.com/crizmo/xlsx-mongo-api.git
npm install
.env
file in the root directory of the project and add the following environment variables: MONGO_URL=<your-mongodb-connection-url>
MONGO_COLLECTION=<your-mongodb-collection-name>
npm start
Contributions are welcome! If you have any ideas, suggestions, or bug fixes, please open an issue or a pull request.
Distributed under the MIT License. See LICENSE for more information.
Contact
Package Maintainer: crizmo on GitHub
Project Link: https://github.com/crizmo/xlsx-mongo-api