How to access the file system from Nuxt.js?

做~自己de王妃 提交于 2020-12-30 08:36:43

问题


In express.js I can write a controller that:

  1. accesses the file system
  2. reads the contents of a directory, and
  3. sends that information as a local variable to the view.

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?


回答1:


To require modules on the serverside, use serverMiddleware

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

Now you can require('fs') in api/index.js



来源:https://stackoverflow.com/questions/46736523/how-to-access-the-file-system-from-nuxt-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!