Tuesday, 21 January 2020

How to call Node from the front end?

Apologies for the noob question. I'm trying to pass a string of file names generated by Node to the front end. I think I have the API endpoint set up... but I'm a little confused about how to set up the request on the front end. Again, very noob question.Here is my server.js code:const http = require('http'); const path = require('path'); const fs = require('fs'); const directoryPath = path.join(__dirname, 'Sounds'); function onRequest(req, res){ res.writeHead(200, {'Content-Type': 'utf8'}); fs.readdir(directoryPath, null, function(error, files) { if (error) { res.writeHead(404); res.write('File not found!'); } else { res.write(files.toString()); } res.end(); }); } http.createServer(onRequest).listen(8000);

Submitted January 21, 2020 at 10:39PM by colinosu

No comments:

Post a Comment