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

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...