I know this is very similar to other questions that have been asked on the same error. In the case I have seen though, the file name had been left off of the url. In my case (as far as I know) the url is specified as it should be and I can see the file on my localhost using other tools.

I have a need in a node.js app to perform I/O on json files without the benefit of using express routing. This is an API that has only one route (processor.js). It is accessed by a menu selection on the GUI by selecting 'Process'. From that point on everything happens within that route including multiple GETs/PUTs to json (for ids to data and then using the ids to get the data) and the building of SQL rows for populating SQL-Server Tables from the parsed json data. That, at least is the concept I am testing now. It is the hand I have been dealt, so I don't have other options.

I am using fs-extra rather than request or axios etc., because they all seem to expect express routes to accomplish the I/O. I appear to be able to directly read and write the json using fs-extra. I am using sequelize (or will be) for the SQL side.

That's the background.

Here is my processor.js (I am merely validating that I can in fact get idsList returned to me at this point):

'use strict';

//  node_modules
const express = require('express');
const router  = express.Router();
const fse     = require('fs-extra')

//  local modules
const idsList  = require('../functions/getIds');

router.get('/', (req, res) => {
    console.log(idsList);
});


module.exports = router;

Here is my getIds function:

'use strict';

//  library modules
const express      = require('express');
const router       = express.Router();
const fse          = require('fs-extra');
const uri          = require('../uri');

//  initialize general variables
let baseURL = `http://localhost:5000${uri}/`;
let idsID = 'ids.json';

const getIds = async () => {
    let url = `${baseURL}${idsID}`;
    try {
        const idsList = await fse.readJson(url);
        console.log('fse.readJson',idsList);
    } catch (err) {
        console.error(err);
    }
}

module.exports = getIds();

And, here is my error, output to the console (it didn't format very well):

Listening on port 5000...
{ [Error: ENOENT: no such file or directory, open 

'http://localhost:5000/Users/doug5solas/sandbox/libertyMutual/playground/api/ids.json'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'http://localhost:5000/Users/doug5solas/sandbox/libertyMutual/playground/api/ids.json' }

What am I missing?

2

There are 2 answers

0
doug5solas On BEST ANSWER

I moved away from fs-extra to fs.readFileSync and solved the problem. It is not my preference. But it does work and the file is small, and only once.

2
Badis Merabet On
  • You can use fs-extra to manipulate files and directories in your local file system only.
  • If you want to read files hosted on other machine over http, try using an http client like: axios.