如何在Qwik Endpoints内访问文件系统?

huangapple go评论64阅读模式
英文:

How can I access file system inside Qwik Endpoints?

问题

我正在尝试在Qwik中创建一个需要访问文件系统的端点。我打算使用Node.js的 fs 模块来实现这个目的。

以下是我编写的代码:

import fs from 'fs'

但是我遇到了这个错误:

(node:248) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Project/SiteQwik/Run.js:1
import fs from 'fs'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

如果我将它改为 const fs = require('fs'),我会得到这个错误:

require is not defined

所以我在这一点上陷入了困境。我该怎么办?

英文:

I'm trying to create an Endpoint in Qwik that needs to access file system. I intend to use Node.js fs for this purpose.

Here's what I wrote:

import fs from 'fs'

And I get this error:

(node:248) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Project/SiteQwik/Run.js:1
import fs from 'fs'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

And if I change it to const fs = require('fs') I get this error:

require is not defined

So I'm stuck at this point. What should I do?

答案1

得分: 1

这是一个创建此端点的工作示例http://xxx/api/config/

要读取的文件:'data/config.json'

{
"version": "1"
}

端点:'src/routes/api/config/index.tsx'

import { type RequestHandler } from '@builder.io/qwik-city';
import { readFileSync } from 'fs';

export const onGet: RequestHandler = async ({ json }) => {
const path = './data/config.json';
const config = readFileSync(path, { encoding: 'utf-8' });
json(200, JSON.parse(config));
};

顺便说一下,对于一个简单的 JSON,您可以使用公共目录来提供它。

英文:

Here is a working example to create this endpoint http://xxx/api/config/

file to read: 'data/config.json'

{
  "version": "1"
}

endpoint: 'src/routes/api/config/index.tsx'

import { type RequestHandler } from '@builder.io/qwik-city';
import { readFileSync } from 'fs';

export const onGet: RequestHandler = async ({ json }) => {
	const path = './data/config.json';
	const config = readFileSync(path, { encoding: 'utf-8' });
	json(200, JSON.parse(config));
};

btw for a simple json you can serve it with the public directory

答案2

得分: 0

以下是要翻译的代码部分:

你能提供更多关于如何操作文件系统的代码吗?

例如,你可以编写加载器如下。该加载器放置在路由文件夹中,并且在文件顶部正常导入。

```ts
import { readFile } from "node:fs/promises"
export const useStaticTextLoader = routeLoader$(async () => {
  try {
    const someTextFromFile = await readFile(YOUR_PATH, { encoding: "utf-8" });

    return someTextFromFile;
  } catch (e) {
    console.log(e);

    return "FALLBACK...";
  }
});
英文:

Could you provide more code on how exactly you want to work with the file system?

For example, you can write loader as such. That loader is placed in the routes folder, and at the top of the file import works normally.

import { readFile } from "node:fs/promises"
export const useStaticTextLoader = routeLoader$(async () => {
  try {
    const someTextFromFile = await readFile(YOUR_PATH, { encoding: "utf-8" });

    return someTextFromFile;
  } catch (e) {
    console.log(e);

    return "FALLBACK..."
  }
});

huangapple
  • 本文由 发表于 2023年6月27日 18:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定