nachocab
nachocab

Reputation: 14404

How to access the file system from Nuxt.js?

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?

Upvotes: 8

Views: 7224

Answers (1)

Hello
Hello

Reputation: 91

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

Upvotes: 9

Related Questions