代码之家  ›  专栏  ›  技术社区  ›  Subhojit

在render.com中使用查询实时运行nodeexpress应用程序会引发内部服务器错误

  •  0
  • Subhojit  · 技术社区  · 3 年前

    我正在尝试使用查询实时运行路由 nodejs express 中的应用 render.com .除搜索过滤器路由外,正在执行与crud操作相关的所有其他路由 url with the query 。在中运行时出现以下错误 render.com

    browser_error_screen

    我在我的实时url中正确地传递了此参数=> https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/?quer=hi ,但它引发了内部服务器错误。如果您检查 https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/ ,它将为storage.js文件中实现的相同router.get方法提供响应。

    此外,该操作正在使用 localhost 。以下是参考资料:

    enter image description here

    我无法调试此问题。如有任何有关此问题的帮助,我们将不胜感激。

    这是我的一段代码:

    /* 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);
    });
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Subhojit    3 年前

    最后 I got the issue resolved

    首先,非常感谢@pierpy。在认真对待你的评论之后,我找到了解决方案。

    问题是=> render.com 正在使用节点版本 14.17.0 我使用的节点版本是 18.16.1

    从日志中识别:

    enter image description here

    因此,具有以下代码的查询实现失败了,因为 node version 14.17.0 无法识别 .replaceAll(" ", "") 从下面的代码中,因为在节点版本中 14.17.0 实施 .replaceAll 可能不存在:

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

    因此,我按照以下步骤找到了解决方案:

    1. 删除了=> node_modules package-lock.json 在的帮助下 rm -rf <folder/filename> 命令(适用于 git bash 在窗口中)
    2. 将节点版本降级为 14.17.0
    3. npm install
    4. 已替换=> .replaceAll(“”,“”) 具有 replace(/\s/g,'')
    5. 推送代码并部署回购。

    如果你们中的任何人在 render.com 通过部署 node-express 应用程序,你可以考虑这种方法并解决这个问题。