0

I have an express server, which works locally, but when I deploy it to App Engine and send a request I get the response that it has been blocked by CORS policy. If I remove the section where I call exec (and move send response) there is no error.

Why could this be happening?

My code:

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { exec } = require('child_process');

const app = express();
app.use(bodyParser.json());
app.use(cors());
module.exports = app

app.post("/", (req,res) =>{ 
    console.log("Get from /"); 
    console.log(req.body.data)

//IF I COMMENT EXEC OUT IT WORKS 
    exec('npx hardhat run scripts/deploy.js --network goerli',
        (error, stdout, stderr) => {
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
            else{
              res.send("response");
            }
        })
                            });


app.listen(8080, () => {
  console.log('listening on port 8080');
});

This is my package.json:

{
  "name": "hardhat-project",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.6",
    "ethers": "^5.6.9",
    "hardhat": "^2.9.9"
  },
  "version": "1.0.0",
  "description": "smart contract",
  "main": "hardhat.config.js",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.18.1",
    "firebase-admin": "^11.0.0"
  },
  "scripts": {
    "start": "node src/index.js",
    "test": "mocha"
  },
  "author": "",
  "license": "ISC"
}

A screenshot of the error: enter image description here

Nina
  • 499
  • 6
  • 16
  • 1
    Your ```exec``` command is trying to run ```hardhat``` but does you App actually install ```hardhat``` in your environment? – NoCommandLine Aug 11 '22 at 17:17
  • @NoCommandLine I npm installed hardhat and then ran npx hardhat to create a Hardhat project. So it's not being installed in my App? What should I do? – Nina Aug 11 '22 at 17:26
  • 1
    Let me rephrase the question. Do you have ```hardhat``` in your ```package.json``` file? When you deploy your app, the Node.js runtime automatically installs all dependencies using the npm install command or, if a yarn.lock file exists, the yarn install command- https://cloud.google.com/appengine/docs/standard/nodejs/specifying-dependencies To be clear, I don't know if GAE supports ```exec``` command or not but if it does and and ```hardhat``` isn't available, you'll still get errors. So I'm trying to eliminate ```hardhat``` as the cause of your error – NoCommandLine Aug 11 '22 at 17:41
  • Also, I have assumed ```hardhat``` is a Node package. If it isn't, then my last comment is moot. – NoCommandLine Aug 11 '22 at 17:45
  • @NoCommandLine I added my package.json to the question. Hardhat is under devDependencies. – Nina Aug 11 '22 at 17:45
  • 1
    According to https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file, ```dev-dependencies``` are used in development/testing while ```dependencies``` are used in production. If GAE is following this, then it won't install your ```hardhat```. You can try to confirm by moving contents of ```dev-dependencies``` into ```dependencies```, redeploy and see if your problem is solved – NoCommandLine Aug 11 '22 at 18:38
  • @NoCommandLine Thank you for helping. I added it, but it's still not working. I get error bloked by cors and then error 500. – Nina Aug 11 '22 at 19:43
  • @NoCommandLine I was able to get rid of exec in my code, but the error persists. So, it's probably Hardhat's fault. – Nina Aug 11 '22 at 21:30
  • 1
    can you add the error to your original post? – NoCommandLine Aug 11 '22 at 21:47
  • @NoCommandLine I added a screenshot of the error. – Nina Aug 11 '22 at 21:56
  • Sorry, I've run out of ideas on this. Hopefully, someone else can help – NoCommandLine Aug 11 '22 at 23:12
  • You need to allow the CORS to communicate with external resources `Access-Control-Allow-Origin: http://example.com `, here is some documentation that can be useful. [1](https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header/35553666#35553666), [2](https://cloud.google.com/storage/docs/cross-origin#bucket-access-external-resource), [3](https://cloud.google.com/functions/docs/writing/write-http-functions#http-example-nodejs) – Andres Fiesco Casasola Aug 12 '22 at 16:03

0 Answers0