Skip to content

Instantly share code, notes, and snippets.

@sjg
Created December 11, 2014 14:02
Show Gist options
  • Save sjg/bb4dfa190781a8f5fd62 to your computer and use it in GitHub Desktop.
Save sjg/bb4dfa190781a8f5fd62 to your computer and use it in GitHub Desktop.
imageServer.js
var imagesnap = require('imagesnap');
var fs = require('fs');
function grabImage(){
var imageStream = fs.createWriteStream('capture.jpg');
imagesnap().pipe(imageStream);
}
setInterval(function(){
grabImage();
}, 5000);
grabImage();
var http = require('http')
var fs = require('fs')
var baseDirectory = __dirname // or whatever base directory you want
var url = require('url');
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
http.createServer(function(req, res) {
// parse url
var request = url.parse(req.url, true);
var action = request.pathname;
// disallow non get requests
if (req.method !== 'GET') {
res.writeHead(405, {'Content-Type': 'text/plain' });
res.end('405 Method Not Allowed');
return;
}
// routes
if (action === '/') {
res.writeHead(200, {'Content-Type': 'text/plain' });
res.end('Hello World \n');
return;
}
// static (note not safe, use a module for anything serious)
var filePath = path.join(__dirname, action);
fs.exists(filePath, function (exists) {
if (!exists) {
// 404 missing files
res.writeHead(404, {'Content-Type': 'text/plain' });
res.end('404 Not Found');
return;
}
// set the content type
var ext = path.extname(action);
var contentType = 'text/plain';
if (ext === '.gif') {
contentType = 'image/gif'
}
res.writeHead(200, {'Content-Type': contentType });
// stream the file
fs.createReadStream(filePath, 'utf-8').pipe(res);
});
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment