我知道这与针对同一错误提出的其他问题非常相似。不过,在我看到的情况下,文件名已从 url 中删除。就我而言(据我所知),url 是按应有的方式指定的,我可以使用其他工具在本地主机上看到该文件。
我需要在 node.js 应用程序中对 json 文件执行 I/O,而无需使用快速路由。这是一个只有一个路由(processor.js)的 API。通过选择“处理”在 GUI 上的菜单选项中访问它。从那时起,所有事情都发生在该路由中,包括多个 GET/PUT 到 json(用于 id 到数据,然后使用 id 来获取数据)以及构建 SQL 行以从解析的 json 数据填充 SQL-Server 表。这至少是我现在正在测试的概念。这是我已经被处理的手,所以我没有其他选择。
我使用的是 fs-extra 而不是 request 或 axios 等,因为它们似乎都期望快速路由来完成 I/O。我似乎能够使用 fs-extra 直接读写 json。我正在为 SQL 端使用 sequelize(或将是)。
这就是背景。
这是我的 processor.js (我只是验证我实际上可以在此时将 idsList 返回给我):
'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;
这是我的 getIds 函数:
'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();
而且,这是我的错误,输出到控制台(它的格式不是很好):
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,代码:'ENOENT',系统调用:'open',路径:' http://本地主机:5000/Users/doug5solas/sandbox/libertyMutual/playground/api/ids.json ' }
我错过了什么?