<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gaurang Parante</title>
    <description>The latest articles on Forem by Gaurang Parante (@gaurang_parante).</description>
    <link>https://forem.com/gaurang_parante</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1557942%2F2a0ebff3-00e8-41e9-a0b2-e55319c1aa33.jpg</url>
      <title>Forem: Gaurang Parante</title>
      <link>https://forem.com/gaurang_parante</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gaurang_parante"/>
    <language>en</language>
    <item>
      <title>How to Integrate Swagger API Documentation into Your Node.js Project</title>
      <dc:creator>Gaurang Parante</dc:creator>
      <pubDate>Wed, 21 Aug 2024 07:46:27 +0000</pubDate>
      <link>https://forem.com/gaurang_parante/how-to-integrate-swagger-api-documentation-into-your-nodejs-project-4ib7</link>
      <guid>https://forem.com/gaurang_parante/how-to-integrate-swagger-api-documentation-into-your-nodejs-project-4ib7</guid>
      <description>&lt;p&gt;Swagger (now OpenAPI) is a popular tool for designing and documenting RESTful APIs. It provides an interactive and user-friendly way to describe your API's endpoints, request parameters, and responses. In this guide, we'll walk through the process of integrating Swagger API documentation into your Node.js project using swagger-jsdoc and swagger-ui-express.&lt;/p&gt;

&lt;p&gt;Step-by-Step Integration&lt;br&gt;
1) Install the Required Packages&lt;br&gt;
First, you need to install the necessary packages. Open your terminal and run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i swagger-jsdoc
npm i swagger-ui-express

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Set Up Swagger in Your server.js&lt;br&gt;
Next, import the packages and configure Swagger in your main server file (server.js):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following code to set up Swagger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Swagger Code
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'API documentation for project',
      version: '1.0.0'
    },
    servers: [
      {
        url: 'http://localhost:3333/'
      }
    ]
  },
  apis: ['./server.js']
}

const swaggerSpec = swaggerJSDoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines the Swagger specification and serves the documentation at the /api-docs endpoint.&lt;/p&gt;

&lt;p&gt;3) Document Your API Endpoints&lt;br&gt;
Now, you can add Swagger comments directly in your server.js file to document your API endpoints.&lt;/p&gt;

&lt;p&gt;For a GET API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @swagger
 *  /api/users:
 *      get:
 *          summary: Get all users
 *          description: Retrieve a list of all users
 *          responses:
 *              200:
 *                  description: Successfully retrieved list of users
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: array
 *                              items:
 *                                  type: object
 *                                  properties:
 *                                      id:
 *                                          type: integer
 *                                          description: The user ID
 *                                      name:
 *                                          type: string
 *                                          description: The user's name
 */

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a POST API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @swagger
 *  /route-name:
 *      post:
 *          summary: This is a POST API for action auth
 *          description: For authentication purposes
 *          requestBody:
 *              required: true
 *              content:
 *                  application/json:
 *                      schema:
 *                          type: object
 *                          properties:
 *                              phone_no:
 *                                  type: string
 *                                  description: The user's phone number
 *                              password:
 *                                  type: string
 *                                  description: The user's password
 *                              form_action:
 *                                  type: string
 *                                  description: Form action
 *                          required:
 *                              - phone_no
 *                              - password
 *          responses:
 *              200:
 *                  description: This API is used to fetch data from the database
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  status:
 *                                      type: boolean
 *                                  message:
 *                                      type: string
 *                                  data:
 *                                      type: object
 */

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace /route-name with your actual route.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Integrating Swagger API documentation into your Node.js project helps streamline API development and improves collaboration. By following these steps, you can easily set up interactive documentation and ensure your API is well-documented and easy to use.&lt;/p&gt;

&lt;p&gt;If you have any questions or need further assistance, feel free to reach out!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Upload an Excel File in Node.js Using Express and Multer</title>
      <dc:creator>Gaurang Parante</dc:creator>
      <pubDate>Sat, 20 Jul 2024 09:16:28 +0000</pubDate>
      <link>https://forem.com/gaurang_parante/how-to-upload-an-excel-file-in-nodejs-using-express-and-multer-ici</link>
      <guid>https://forem.com/gaurang_parante/how-to-upload-an-excel-file-in-nodejs-using-express-and-multer-ici</guid>
      <description>&lt;p&gt;I'm trying to upload an Excel file in my Node.js application using Express, Multer, and the XLSX library. I've put together the following setup, but I want to ensure I'm doing it correctly and efficiently. Here is my code:&lt;/p&gt;

&lt;p&gt;server.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const multer = require('multer');
const xlsx = require('xlsx');
const path = require('path');
const fs = require('fs');

const app = express();

// Serve the HTML file for the upload form
app.get('/', (req, res) =&amp;gt; {
    res.sendFile(path.join(__dirname, 'index.html'));
});

// Multer configuration
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads'); // Uploads directory
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});

const upload = multer({
    storage: storage,
    fileFilter: function (req, file, cb) {
        const ext = path.extname(file.originalname);
        if (ext !== '.xlsx' &amp;amp;&amp;amp; ext !== '.xls') {
            return cb(new Error('Only Excel files are allowed'));
        }
        cb(null, true);
    }
});

// Route to handle file upload
app.post('/upload', upload.single('excel'), (req, res) =&amp;gt; {
    try {
        if (!req.file) {
            return res.status(400).send('Please upload an Excel file');
        }

        // Process uploaded file
        const filePath = req.file.path;
        const workbook = xlsx.readFile(filePath);
        const sheetName = workbook.SheetNames[0]; // Assuming it's the first sheet

        // Reading the first sheet
        const worksheet = workbook.Sheets[sheetName];
        const data = xlsx.utils.sheet_to_json(worksheet);
        console.log(data);

        // Redirect to the main page
        res.redirect('/');
    } catch (error) {
        console.error(error);
        res.status(500).send('Error processing file');
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Upload Excel&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Upload Excel File&amp;lt;/h1&amp;gt;
    &amp;lt;form action="/upload" method="post" enctype="multipart/form-data"&amp;gt;
        &amp;lt;input type="file" name="excel" accept=".xls, .xlsx" required&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Upload&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
Express: Handles HTTP requests and serves static files (index.html).&lt;br&gt;
Multer: Middleware for handling multipart/form-data, used here for file uploads.&lt;br&gt;
XLSX: Library for parsing Excel files.&lt;br&gt;
Path and FS: Node.js modules for handling file paths and system operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;br&gt;
HTML Form: Provides a form for users to upload Excel files.&lt;br&gt;
Multer Configuration: Sets up Multer to save uploaded files in the ./uploads directory and filter for .xlsx and .xls file types.&lt;br&gt;
File Upload Route: Handles the file upload, reads the Excel file using xlsx.readFile, extracts data from the first sheet, and logs the data to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Questions:&lt;/strong&gt;&lt;br&gt;
Is this the best way to handle Excel file uploads and processing in Node.js?&lt;br&gt;
Are there any improvements or best practices I should consider for error handling and security?&lt;br&gt;
Any advice or suggestions would be greatly appreciated!&lt;/p&gt;

</description>
      <category>node</category>
      <category>multer</category>
      <category>express</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
