Skip to content Skip to sidebar Skip to footer

Webcam Doesn't Display When Node.js It's Running

I have a script that takes a picture from my webcam. it's working fine when i runs locally or when i see in a online server. But when i run the html file from the node.js, it doesn

Solution 1:

Your program seems to be sending the content of index.html for every request (for html, icons, scripts, etc.). Maybe try using express to serve static files properly:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname));

app.listen(3000, function(){
  console.log('Executando Servidor HTTP');
});

It's even easier than the way you want to write it, because to make your script work you would have to manually route the requests based on the request object to properly send scripts and different assets, make sure to handle MIME types, paths like ../.. etc.

Also you may want to move your static files (html, css, js) to a different directory like static and change the app.js like this:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/static'));

app.listen(3000, function(){
  console.log('Executando Servidor HTTP');
});

so that no one will be able to get your app.js code by browsing to: http://localhost:3000/app.js

Post a Comment for "Webcam Doesn't Display When Node.js It's Running"