Node.js net::ERR_FAILED 503 .... sometimes

35 views Asked by At

I am banging my head against the wall for a couple of days now with no result, except a slightly disfigured wall and head...

I am using node.js with express and react. I am sending a zip file from react. The api end point should do the following jobs:

  • unzip the file,
  • run a cmd command on the containing files (which will create a new file),
  • read the new file as geojson and
  • send the results to the frontend.

In localhost it works fine. In the server, however, there are some issues, which appear sometimes, while other times it is working fine. So, with the same exactly file, sometimes the process will conclude fine and others will not run all the jobs (stopping after the first, second or third job) and send a net::ERR_FAILED 503 along with blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource (which I don't think has anything to do with the issue).

Might have something to do with not waiting for a job to complete before starting the next? Something in the server environment? The server is running wamp. I didn't set it up, but I can access it.

The code in question in node.js is the following:

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

Can anybody understand what could be the issue? If you need any more data please let me know. Thank you in advance.

0

There are 0 answers