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
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:
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
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 version14.17.0
and the node version I was using is18.16.1
.Identified from the logs:
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 version14.17.0
the implementation of.replaceAll
is might not be present:Hence I followed the below steps to get to the solution:
node_modules
andpackage-lock.json
with the help ofrm -rf <folder/filename>
command(applicable for git bash in windows)14.17.0
npm install
.replaceAll(" ", "")
withreplace(/s/g,'')
If any of you are getting the same issue with
render.com
by deploying thenode-express
app, you can follow the same and get rid of the issue.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.