skip to Main Content

I’m trying to run a route live with query using nodejs express app in render.com. All other routes related to the crud operations are getting executed except the search filter route url with the query. I’m getting the below error while running it in render.com

browser_error_screen

I’m passing this parameter correctly in my live url => https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/?quer=hi, but its throwing the internal server error. If you check the https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/, it would give the response for the same router.get method implemented in the storage.js file.

Moreover, the operation is getting executed with localhost. Below is the reference:

enter image description here

I’m unable to debug this issue. Any help regarding the issue would be appreciated.

Here’s my piece of code:

/* app.js */
const express = require('express');
const bodyParser = require('body-parser');

// create express app
const app = express();
const port = process.env.PORT || 3000;
const cors = require('cors');

// set middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

// import routes
const routes = require('./routes/Routes');
app.use('/', routes);

//start server
app.listen(port, () => {
    console.log(`Server is up on port: ${port}`);
});


/* storage.js */
storageRoutes.get(storageUrl, (req, res) => {
    const { type } = req.params;
    const { quer } = req.query;
    // get existing data collection// fetch the collection from the storage related to the products
    let existingDataCollection = getData(storagePath);
    // get the existing data-set fot the specific type
    let existingCollectionTypeData = [...existingDataCollection[type]];

    // search filter implementation based on query
    if (quer) {
        // filter query
        const query = quer.toLowerCase().replaceAll(" ", "");
        // get the data-object related to the specified type
        existingCollectionTypeData = existingCollectionTypeData.filter((element) =>
            element.name.replaceAll(" ", "").toLowerCase().includes(query)
        );
    }

    // send the response with the collection of specified types
    res.send(existingCollectionTypeData);
});

2

Answers


  1. Chosen as BEST ANSWER

    Firstly, Thanks a lot @pierpy. I found the solution after taking your comment a bit serious.

    The issue was render.com is using the node version 14.17.0 and the node version I was using is 18.16.1.

    Identified from the logs:

    enter image description here

    Hence, my query implementation which had the below code was getting failed because the node version 14.17.0 was not able to recognize .replaceAll(" ", "") from the below code, because in the node version 14.17.0 the implementation of .replaceAll is might not be present:

    // search filter implementation based on query
    if (quer) {
        // filter query
        const query = quer.toLowerCase().replaceAll(" ", "");
        // get the data-object related to the specified type
        existingCollectionTypeData = existingCollectionTypeData.filter((element) =>
            element.name.replaceAll(" ", "").toLowerCase().includes(query)
        );
    }
    

    Hence I followed the below steps to get to the solution:

    1. Remove => node_modules and package-lock.json with the help of rm -rf <folder/filename> command(applicable for git bash in windows)
    2. Downgraded my node version to 14.17.0
    3. npm install
    4. replaced .replaceAll(" ", "") with replace(/s/g,'')
    5. pushed the code and deployed the repo back.

    If any of you are getting the same issue with render.com by deploying the node-express app, you can follow the same and get rid of the issue.


  2. You’ve set CORS headers manually in your server-side code. However, Express.js has a built-in package called cors that handles this for you. Consider using this package if you aren’t already, as it may resolve any potential access control issues.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search