Jump to content

Help with using local resources node js


Recommended Posts

So I followed the tutorial to control an rgb led from a web server on a raspberry pi (https://www.w3schools.com/nodejs/nodejs_raspberrypi_rgb_led_websocket.asp). However I am trying to use an image stored in the directory of the HTML to use as a button rather than an online image hosting service. I have tried using express.static to no avail I think it might be conflicting with the http server that is used in the tutorial. Any help would be greatly appreciated. 

Link to comment
Share on other sites

Open your browser's developer tools and look at the Network tab to see the requests.

Unless you're using a web browser to just open a local file (e.g. File -> Open), then you're using a web server.  

Link to comment
Share on other sites

I don’t think I’ve clearly explained what I’m asking, my webserver using node is running as a http server which from what I guess will only serve specific files you direct it to read etc. I have multiple files inside the public folder I need to serve but primarily two images, the HTML file for the server is located in the same directory as these images but can’t seem to serve them so that the HTML can access the contents. 

Link to comment
Share on other sites

You'll need to parse the path, and then check the file type it relates to and serve it accordingly.

To serve an image in addition to your path you could try something like this:

fs = require('fs');
http = require('http');
url = require('url');

http.createServer(function(req, res){
  var request = url.parse(req.url, true);
  var action = request.pathname;

  if (action == '/logo.gif') {
     var img = fs.readFileSync('./logo.gif');
     res.writeHead(200, {'Content-Type': 'image/gif' });
     res.end(img, 'binary');
  } else { 
     res.writeHead(200, {'Content-Type': 'text/plain' });
     res.end('Hello World \n');
}).listen(8080, '');


Link to comment
Share on other sites

  • 2 months later...

What Funce said.. and thank you Funce.. that may be the solution to my issue as well. I discovered quite quickly that the webserver.js provided in that tutorial ignores the actual url and simply sends the single specified index.html  in the code no matter what you request.. 

 Unfortunately having worked around the issue it caused me, I'm left with a different issue for my solution.. for which I am about to start a thread...Also even Funce's solution looks to provide a limited fix wherein you'd have to manually list any and every file you want it to be able to send as a different action, but it is a start. 




Edited by JoeGreene
grammar failures.
Link to comment
Share on other sites

Check that, Using Funce's solution and if(){}else if(){} I added an action for each file I needed it to serve up and it resolved the errors I'd been seeing and now I shall continue my adventure into making a raspberry pi with a relay control board  make my Handicapped Van Wheelchair lift phone controllable.. then I can rule the world from my wheeelchair!!!!!! (Or just make my own custom wheelchair controller... one step at a time....The key issue was my pi will be a secured WAP which I can connect my phone to and use my browser to control the functions of the gpio to activate the relays thus opening the doors, unfolding, folding  and raising/lowering the wheelchair lift.. MUAHAHAH.. with enough gpio left over to control 2 more relays Can we say DISCO Lights!! (just kidding about the disco lights)


Link to comment
Share on other sites

You've no idea how happy this made me. Truly. Thank you Funce for an elegant adaptable fix to this!!I confirm it is all now working.. except for realy 6 which is being a schmuck and is probably a bad relay, loose wire or blown gpio pin.. all of which are correctable by other means. 


  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...