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);
});