我有一个脚本可以从我的摄像头上拍到一张照片。当我在本地运行或在在线服务器上看到时,它运行得很好。
但是当我从node.js运行html文件时,它不会显示来自我的摄像头的视图。
怎么解决这个问题?
节点中的我的服务器:
// app.js
var http = require('http');
var fs = require('fs');
sys = require('util');
var server = http.createServer(function(request, response){
fs.readFile(__dirname + '/index.html', function(err, html){
console.log("oi");
response.writeHeader(200, {'Content-Type': 'text/html'});
response.write(html);
response.end();
});
});
server.listen(3000, function(){
console.log('Executando Servidor HTTP');
});
我的HTML:
<!DOCTYPE html>
<html>
<head>
<title>Javascript Webcam Demo - <MyCodingTricks/></title>
</head>
<body>
<h3>Demonstrates simple 320x240 capture & display</h3>
<div id="my_camera"></div>
<!-- A button for taking snaps -->
<form>
<input type=button class="btn btn-success" value="Take Snapshot" onClick="take_snapshot()">
</form>
<div id="results" class="well">Your captured image will appear here...</div>
<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="2/webcam.min.js"></script>
<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#my_camera' );
function take_snapshot() {
// take snapshot and get image data
Webcam.snap( function(data_uri) {
// display results in page
document.getElementById('results').innerHTML =
'<h2>Here is your image:</h2>' +
'<img src="'+data_uri+'"/>';
Webcam.upload( data_uri, 'upload.php', function(code, text) {
// Upload complete!
// 'code' will be the HTTP response code from the server, e.g. 200
// 'text' will be the raw response content
});
} );
}
</script>
</body>
</html>
发布于 2016-06-02 23:48:58
您的程序似乎在为发送的内容--每个请求(用于html、图标、脚本等)。也许可以尝试使用express
来正确地服务静态文件:
var express = require('express');
var app = express();
app.use('/', express.static(__dirname));
app.listen(3000, function(){
console.log('Executando Servidor HTTP');
});
它甚至比您想要编写它的方式更容易,因为要使您的脚本工作,您必须基于request
对象手动路由请求,以正确地发送脚本和不同的资产,确保处理MIME类型、路径(如../..
等)。
另外,您可能希望将静态文件(html、css、js)移动到类似于static
的不同目录中,并按以下方式更改app.js
:
var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/static'));
app.listen(3000, function(){
console.log('Executando Servidor HTTP');
});
这样,就没有人能够通过浏览到:app.js
来获取您的http://localhost:3000/app.js代码。
https://stackoverflow.com/questions/37603778
复制相似问题