You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js+Express+React上传Zip文件偶发503错误排查求助

问题排查:Node.js+Express上传Zip文件流程在WAMP服务器间歇性失败

使用Node.js+Express+React技术栈,前端上传Zip文件至API接口,接口需完成以下操作:

  • 解压文件
  • 对解压后的文件执行cmd命令生成新文件
  • 将新文件读取为GeoJSON格式
  • 将结果返回至前端

本地环境下该流程运行正常,但部署在WAMP服务器上时,同一文件有时能正常完成全部流程,有时会在某一步骤中断,返回net::ERR_FAILED 503错误,同时附带“blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource”提示(本人认为此提示与核心问题无关)。

怀疑问题可能源于:

  • 未等待前一操作完成就执行下一步
  • 服务器环境配置差异

相关Node.js代码如下:

exports.uploadLayer = async (req, res) => {
if (req.files) {
    const layerType = req.body.layerType;
    const fileNames = req.files.map((file) => file.filename);

    const zipFileName = fileNames[0];

    const uploadsFolder = path.join(__dirname, '../../uploads');
    const zipFilePath = path.join(uploadsFolder, zipFileName);
    const zipFileNameNoExt = zipFileName.substring(0, zipFileName.lastIndexOf('.')) || zipFileName;
    const newDir = path.join(uploadsFolder, `${zipFileNameNoExt}`);

    if (!fs.existsSync(newDir)) fs.mkdirSync(newDir);

    let extensionsExist = {};
    let shpFileName = '';
    let shpFileNameNoExt = '';

    try {
        const files = await decompress(zipFilePath, newDir);

        // Delete zip file
        fs.unlinkSync(zipFilePath);

        // Get if all necessary extentions exist
        files.forEach((file) => {
            const ext = path.extname(file.path);
            extensionsExist[ext] = true;
            if (ext === '.shp') {
                shpFileName = file.path;
                shpFileNameNoExt = shpFileName.substring(0, shpFileName.lastIndexOf('.')) || shpFileName;
            }
        });

        if (extensionsExist['.shp'] && extensionsExist['.shx'] && extensionsExist['.dbf'] && extensionsExist['.prj']) {
            const geojsonFile = path.join(newDir, `${zipFileNameNoExt}.geojson`);
            const input = path.join(newDir, `${shpFileName}`);
            const prjFile = path.join(newDir, `${shpFileNameNoExt}.prj`);

            const options = layerType === 'line' || layerType === 'polygon' ? '-nlt PROMOTE_TO_MULTI' : '';

            const cmd = `ogr2ogr -f "GeoJSON" ${options} -s_srs "${prjFile}" -t_srs EPSG:4326 "${geojsonFile}" "${input}"`;
            cp.execSync(cmd);

            fs.readFile(geojsonFile, function (err, data) {
                if (err) {
                    fs.rmSync(newDir, { recursive: true, force: true });
                    return res.status(500).json('There was some issue');
                } else {
                    const geojson = JSON.parse(data);
                    const features = geojson['features'];

                    // No features
                    if (features.length === 0) {
                        fs.rmSync(newDir, { recursive: true, force: true });
                        return res.status(500).json('No features');
                    }

                    // Get geometry type
                    const firstFeature = features[0];
                    let geometryType = firstFeature.geometry?.type;

                    // No data
                    if (!geometryType) {
                        fs.rmSync(newDir, { recursive: true, force: true });
                        return res.status(500).json('No data');
                    }

                    const properties = firstFeature.properties;
                    const fieldOptions = Object.keys(properties);

                    const returnData = {
                        fieldOptions: fieldOptions,
                        originalFilename: zipFileName,
                        geojson: geojson,
                    };

                    // Remove unzip folder
                    fs.rmSync(newDir, { recursive: true, force: true });

                    return res.status(200).json(returnData);
                }
            });
        } else {
            if (fs.existsSync(newDir)) fs.rmSync(newDir, { recursive: true, force: true });
            return res.status(500).json('Missing files');
        }
    } catch (err) {
        if (fs.existsSync(newDir)) fs.rmSync(newDir, { recursive: true, force: true });
        return res.status(500).json('There was some issue');
    } finally {
        if (fs.existsSync(newDir)) fs.rmSync(newDir, { recursive: true, force: true });
    }
} else {
    return res.status(500).json('No file');
}}

内容的提问来源于stack exchange,提问作者gijo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 12:56:07