I'm trying to get an array from node.js
(server-side) to react app.js
(client-side) so I need to get files to array from node and send it to app.js
it seems like module.exports
from node and import from app.js
does not work like this here is what I tried
node.js
file:
const fs= require('fs');
const files=fs.readdirSync('../Movies');
module.exports={
movies:files
}
app.js
file
import { movies } from "./server/server.js";
console.log(movies);
do have have to
You can't do it. app.js
in client, It can't import "./server/server.js"
But you can send array has name of file in that forder when render.
node.js
:
const files=fs.readdirSync('../Movies');
// when user request your page (this example use ejs)
res.render('<your_ejs_file>', {
myFiles: files,
});
// in your_ejs_file, create variable:
<script>
var yourFiles = <%= JSON.stringify(myFiles)%>
</script>
yourFiles
is array named of file in your_folder
If you want load file from server, you can use AJAX to load it.
I use name of variable diffrent to you avoid mistake them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.