0

I have a project using express, axios and fs.writeFileSync cause ERR_CONNECTION_REFUSED err.

For demo purpose, I made this repo, the architecture / idea is the same, but I am not able to replicate the same err, but it is only for you to understand what I try to achieve. Following error message are from the real project, but I modify it to align with the demo repo.

in chrome console

getDetail error Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:83)


xhr.js:178 GET http://localhost:3001/detail net::ERR_CONNECTION_REFUSED
dispatchXhrRequest  @   xhr.js:178
xhrAdapter  @   xhr.js:12
dispatchRequest @   dispatchRequest.js:52
Promise.then (async)        
request @   Axios.js:61
Axios.<computed>    @   Axios.js:76
wrap    @   bind.js:9
(anonymous) @   detailApi.js:13
step    @   tslib.es6.js:100
(anonymous) @   tslib.es6.js:81
(anonymous) @   tslib.es6.js:74
__awaiter   @   tslib.es6.js:70
getDetail   @   detailApi.js:4
runCallEffect   @   redux-saga-core.esm.js:524
runEffect   @   redux-saga-core.esm.js:1204
digestEffect    @   redux-saga-core.esm.js:1271
next    @   redux-saga-core.esm.js:1161
currCb  @   redux-saga-core.esm.js:1251
runSelectEffect @   redux-saga-core.esm.js:731
runEffect   @   redux-saga-core.esm.js:1204
digestEffect    @   redux-saga-core.esm.js:1271
next    @   redux-saga-core.esm.js:1161
currCb  @   redux-saga-core.esm.js:1251
(anonymous) @   redux-saga-core.esm.js:481
exec    @   redux-saga-core.esm.js:31
flush   @   redux-saga-core.esm.js:87
asap    @   redux-saga-core.esm.js:46
chan.put    @   redux-saga-core.esm.js:375
(anonymous) @   redux-saga-core.esm.js:1412
dispatch    @   VM99:1

in network tab

Headers
====
Request URL: http://localhost:3001/detail
Referrer Policy: strict-origin-when-cross-origin

Accept: */*
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: GET
Origin: http://localhost:3000
Sec-Fetch-Mode: cors

Some findings and code below

  • If I remove fs.writeFileSync or change it to a delay loop, no network err. Everything is good.

  • Use either fs.writeSync or fs.writeFileSync network cause network err

const cors = require("cors");
const express = require("express");
const fs = require("fs");
const path = require("path");
const bodyParser = require("body-parser");

function server() {
  const server = express();

  server.use(cors());
  server.use(bodyParser.json());

  server.get("/detail", (req, res) => {
    const data = require("./detail.json");
    res.status(200).jsonp(data);
  });

  server.post("/agree", (req, res) => {
    try {
      const agree = req.body.agree;
      const filePath = path.resolve(__dirname, "./agree.json");
      let data = JSON.parse(fs.readFileSync(filePath));
      data.agree = agree;

      // * If I remove it or change it to a delay loop, no net work err
      // * fs.writeSync or fs.writeFileSync network cause err
      fs.writeFileSync(filePath, JSON.stringify(data));

      res.status(200).jsonp({ success: true });
    } catch (error) {
      throw new Error("error", error);
    }
  });

  return server;
}

module.exports = server;
kenpeter
  • 7,404
  • 14
  • 64
  • 95

2 Answers2

0

if you are using nodemon, after writeFile() it restarts everything. so basically nodemon is watching for changes and it will restart when file changed. if you want that nodemon will not watch json file (for example, like in my case) use --ext js on start script or check this answer: Looks like when I do fs.writeFile(), the changed file restarts nodemon. How to make it not restart?

or try to use node server.js

INC
  • 31
  • 3
0

It seems like the server is not listening and the function is not exported correctly. You need to call the function in order to wrap and return all the handlers of the server constant.

function server() {
   const server = express();

   server.use(bodyParser.json());
   // just testing the post endpoint

   server.post("/agree", (req, res) => {
     try {
         const agree = req.body.agree;
         const filePath = path.resolve(__dirname, "./agree.json");
         let data = JSON.parse(fs.readFileSync(filePath));
         data.agree = agree;

         fs.writeFileSync(filePath, JSON.stringify(data));

         res.status(200).jsonp({ success: true });
     } catch (error) {
        throw new Error("error", error);
     }
  });

  server.listen(3000, ()=> console.log("Listening"));

  return server;
}

module.exports = server();

Test:

enter image description here

rags2riches-prog
  • 1,663
  • 1
  • 10
  • 22