Jump to content
disease

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. 

Share this post


Link to post
Share on other sites

I don't know what express.static is, but if the device already has a web server you don't want to run another one.  If the file is in the same directory then just write the filename in the HTML.

Share this post


Link to post
Share on other sites

Have tried this,  unfortunately the http is preventing this from happening it seems it’s not creating a local host it’s just serving the specific HTML file. My Code is here if you want to take a look

Share this post


Link to post
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.  

Share this post


Link to post
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. 

Share this post


Link to post
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, '127.0.0.1');

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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)

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...