如何从 Nuxt.js 访问文件系统? [英] How to access the file system from Nuxt.js?

查看:70
本文介绍了如何从 Nuxt.js 访问文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 express.js 中,我可以编写一个控制器:

In express.js I can write a controller that:

  1. 访问文件系统
  2. 读取目录的内容,以及
  3. 将该信息作为局部变量发送到视图.

我不知道如何在 Nuxt.js 中解决这个问题,因为我不能从组件中要求 fs 模块.是否有任何地方可以使用服务器中的文件列表填充变量(例如,在 static 文件夹中),以便组件可以访问它?

I'm not sure how to go about this in Nuxt.js because I can't require the fs module from the component. Is there anywhere where I can populate a variable with a list of files in the server (for example, in the static folder) so that the component has access to it?

推荐答案

要在服务器端要求模块,请使用 服务器中间件

To require modules on the serverside, use serverMiddleware

# nuxt.config.js
module.exports = {
  serverMiddleware: [
    { path: '/api', handler: '~/api/index.js' }
  ]
}

现在你可以在api/index.js

这篇关于如何从 Nuxt.js 访问文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆