Imgur or PlantNet API for website working on local host but giving POST 500 Internal Service Error when deployed on Render

I am a beginner at working with APIs, full-stack applications, and JavaScript and wanted to use Imgur’s API to upload the image and get a URL and then use PlantNet’s API to identify the plant. The code works perfectly well on the local host but when deployed on Render, gives the error POST: 500 Internal service error and does not even give any of the console log error messages I used in an effort to debug. I am confused on what to do an how to fix this issue. Is it an Imgur API issue or something else?

Below is my code for the server side using JavaScript:

import 'dotenv/config';
import express from 'express';
import multer from 'multer';
import fetch from 'node-fetch';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import axios from 'axios';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express();
const port = 3000;

const uploadDir = path.join(__dirname, 'uploads');

if (!fs.existsSync(uploadDir)) {
    fs.mkdirSync(uploadDir, { recursive: true });
    console.log('Uploads directory created:', uploadDir);
} else {
    console.log('Uploads directory exists:', uploadDir);
}

fs.access(uploadDir, fs.constants.W_OK, (err) => {
    if (err) {
        console.error(`Uploads directory is not writable: ${err}`);
    } else {
        console.log('Uploads directory is writable');
    }
});

const upload = multer({ dest: uploadDir });

app.use(express.static(path.join(__dirname, 'public')));

//GET request from PlantNet API
async function identifyPlant(project, imageUrl) {
    const apiKey = process.env.PLANTNET_API_KEY;
    const url = new URL(`https://my-api.plantnet.org/v2/identify/${project}`);

    url.searchParams.append('images', imageUrl);
    url.searchParams.append('organs', 'auto');
    url.searchParams.append('include-related-images', 'true');
    url.searchParams.append('no-reject', 'false');
    url.searchParams.append('lang', 'en');
    url.searchParams.append('type', 'kt')
    url.searchParams.append('api-key', apiKey);

    try {
        const response = await fetch(url, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }
        });

        if (!response.ok) {
            throw new Error('Failed to fetch data');
        }

        const data = await response.json();
        console.log('Success:', JSON.stringify(data, null, 2));

        if (!Array.isArray(data.results)) {
            console.error('data.results is not an array:', data);
            return [];
        }

        const results = data.results.map(result => {
            const images = Array.isArray(result.images) ? result.images.map(image => image.url.o) : [];
            return {
                score: result.score,
                species: {
                    scientificName: result.species.scientificName,
                    genus: result.species.genus.scientificName,
                    family: result.species.family.scientificName,
                    commonNames: result.species.commonNames
                },
                images: images
            };
        });
        return results;
    } catch (error) {
        console.error('Error identifying plants:', error);
    }
}

// using Imgur API
async function uploadImage(imagePath) {
    const clientId = process.env.CLIENT_ID_IMGUR;

    try {
        const image = fs.readFileSync(imagePath, { encoding: 'base64'});
        console.log('Uploading image to Imgur')

        const response = await axios.post('https://api.imgur.com/3/image', {
            image: image,
            type: 'base64'
        }, {
            headers: {
                Authorization: `Client-ID ${clientId}`
            }
        }); 

        if (response.data && response.data.data && response.data.data.link) {
            console.log('Imgur API response:', response.data);
            return response.data.data.link;
        } else {
            throw new Error('Failed to get image link from Imgur');
        }
    } catch (error) {
        console.error('Error uploading image:', error);
        throw error;
    }
}


app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.post('/uploads', upload.single('image'), async (req, res) => {
    const project = 'all';

    const imagePath = req.file.path;
    console.log('Image received:', imagePath)

    try {
        const imageUrl = await uploadImage(imagePath);
        console.log('Image uploaded to Imgur:', imageUrl)

        const result = await identifyPlant(project, imageUrl);
        console.log('Plant identified:', result)

        fs.unlinkSync(imagePath);
        res.json(result);
    } catch (error) {
        console.log('Error in /uploads endpoint:', error);
        fs.unlinkSync(imagePath);
        res.status(500).send('Error identifying plant');
    }
});

app.listen(port, () => {
    console.log(`Server listening at http://localhost:${port}`);
});

Again, I tried to understand the issue by using console.log messages but they do not show up at all and I only get 500: Internal service error, failed to upload image (which was another console.log message I added in another script if I could not get a proper response from Imgur). On the local host, everything works fine and the plant identifications are listed.
Any help is appreciated.

New contributor

Aaaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Make sure that your environment variables (CLIENT_ID_IMGUR and PLANTNET_API_KEY) are correctly set in Render.

  • Render: Check the Render dashboard to ensure that the environment
    variables are set correctly for your deployed service.

Verify your Render deployment configuration to ensure it’s correctly set up. This includes build and start commands, the base directory, and any necessary build scripts.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật