سؤال

Please no more downvotes. I realize how stupid this question was now, but the answer might help someone else that's new, so I'm not going to remove it.

Alright, so this might seem stupid, but I just need a quick nudge, and I'll be able to wrap my head around it...

So, I know you can't just run the Node API in the browser (without something like browserify), and that's fine. I understand that bit.

What I don;t understand is how I'm supposed to access the Node API from within my application. I have a pretty extensive JS file that contains my application's functions. However, if I try to require the FS module, it says that require is undefined. I understand that as well, but how, then, can I make a call to the Node API from my app?

tl;dr: How can I use the filesystem from within my public JS files (that run in-browser). How do I set up some sort of function that, when I call it (in browser), it makes the server use the fs, then gives the results to the browser?

هل كانت مفيدة؟

المحلول

The key thing to understand is that what you have is actually essentially two different applications, and those applications talk to eachother via network requests. For what you are describing, generally your Node server would expose an API, which you then trigger via AJAX requests from the in-browser JS code.

You can read up on exposing API methods in Node here: http://blog.modulus.io/nodejs-and-express-create-rest-api

For the client side of things, you should read about $.ajax and its arguments.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top