I’m new to this so please be patient. I’m creating a node,js app in WebStorm and have hit a bit of a problem. I have created a js called sparkfuncs.js which holds all the login code and functions to retrieve values. This works great server side, but I’d also like to call some if the higher level functions from the browser.
The reason for writing the sparkfuncs.js file was to centralise all spark related functions and also hide the access tokens and device Ids from the user. For example I’m not happy with the web page java script:
As this publishes the data to the user that I want to hide. The problem I’ve come across in my script is the require statement eg:
var spark = require(‘spark’);
I’ve done some reading around this and it’s a server side function which is not supported by the browser. I’ve taken a look at a script called requirejs but can’t get my head around it.
Has anyone else managed to run server side JavaScript code from the browser which contains the require function?
Not really, but you don't have to. There's a browser version available for you to use. It's even mentioned in the docs: https://docs.particle.io/reference/javascript/#client-side
There's even an HTML example included: Particle Login Example (change the version to 1.0.0 if you wish)
That way, you don't even have to struggle with the URL's anymore, and can just call the functions. There's even a login method included which you can use so that you don't have to hard-code credentials.
Have a look at the library, and let me know if you need further assistance.
Hi and thanks very much for your quick reply. The CDN is a step in the right direction, but I don’t want the user to have to login the the particle site as this needs to be transparent. I want to hard code the access token and dev ids on the server side and be able to call a simple server function from the web page like sparkfuncs.getvar1() which will return a value back to the page.
That’s possible as well. You could hardcode the credentials on the server side, and use those to authenticate with Particle. I’m not sure if there’s still the possibility of using the library on the client side, since it’ll have to login to the cloud somehow. If you’re willing to pass the credentials from the server to the client side, and use those to login, you should be okay I guess. I’m not skilled enough in the whole Javascript area to know if there’s a another way you could authenticate the client side.
Alternatively, you’d have to create your own API on your server, which you can call on the client side. Your server should then handle the communications with the Cloud. I think that’s the most common scenario if you want your server to be in control, while allowing clients to control certain aspects. (Does that make sense?)
Yes that makes perfect sense thanks and it’s the way I’ve been thinking. I checked out a couple of wrapper apps called requirejs and browserify but these seem to wrap the server code in browser friendly code, but actually expose all the functions and id tokens etc. to the browers which isn’t what I want.
I’ll have a further think about this but I think what you’re suggesting is the right way to go.