Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the file system from Nuxt.js?

Tags:

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?

like image 585
nachocab Avatar asked Oct 13 '17 19:10

nachocab


People also ask

How do I get the path in Nuxt?

To get the current route path in nuxt, we can use this. $route. path or $this. $nuxt.

What is the .Nuxt directory?

nuxt directory is the so-called build directory. It is dynamically generated and hidden by default. Inside the directory you can find automatically generated files when using nuxt dev or your build artifacts when using nuxt build .

How does SSR work in Nuxt?

Server-side rendering (SSR), is the ability of an application to contribute by displaying the web-page on the server instead of rendering it in the browser. Server-side sends a fully rendered page to the client; the client's JavaScript bundle takes over which then allows the Vue.

Can I use vue3 with Nuxt?

It's only right that Nuxt 3 was built to support Vue 3 features. Vue 3 was released in October 2020 and has since garnered a lot of praise from the Vue community. With Nuxt 3 being written in Vue 3, you get access to features like the Composition API, better module imports, and overall improved app performance.


1 Answers

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

like image 113
Hello Avatar answered Sep 21 '22 14:09

Hello