Skip to content

Instantly share code, notes, and snippets.

@nanoman657
Last active January 26, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoman657/39d306bcca9bad4fb24044f32d26b8cb to your computer and use it in GitHub Desktop.
Save nanoman657/39d306bcca9bad4fb24044f32d26b8cb to your computer and use it in GitHub Desktop.
Testing
'use strict';
var http = require('http');
var fs = require('fs');
//var JavaScript1 = require('./prep4Firebase') //require('./JavaScript1');
//var d = require('./Script1');
var url = require('url');
var port = process.env.PORT || 1337;
//const vision = require('@google-cloud/vision');
// Imports the Google Cloud client library
async function quickstart(image) {
image = image || 'https://pdfexpert.com/img/howto/fill-w2-form/2019-how-to-fill-w2-filledform.png'
// Imports the Google Cloud client library
// Creates a client
/*
const client = new vision.ImageAnnotatorClient({keyFilename: './whatthepassword.json'});
// Performs label detection on the image file
const [result] = await client.textDetection(image);
const detections = result.fullTextAnnotation.text;
console.log(detections)
*/
return 'detections'
}
http.createServer(function (req, res) {
var q = url.parse(req.url, true); // Create query
var filename = '.' + q.pathname; // Define the filename
let body = [];
req.on('error', (err) => {
console.error(err);
}).on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
// At this point, we have the headers, method, url and body, and can now
// do whatever we need to in order to respond to this request.
});
if (req.method == "GET") {
if (filename !== './vision' && filename !== './') {
fs.readFile(filename, function (err, data) {
// Create 404 response
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end('404 Not Found' + ':' + filename);
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end('');
});
} else if (filename === './vision') {
quickstart().then(value => { res.end(value) }).catch(value => { console.log('The image couldn\'t be parsed') }); // Value is whatever the output of the promise is. could be named anything.
}
else { res.end('Crappy Home page') }
} else if (req.method == "POST") {
//var request_match = req.url.match(/Image=(.*)&(.*)/);
//var image_url = req.url.match(/Image=(.*)&/)[1];
//var Anotherkey = req.url.match(/Image=(.*)&/)[2];
// Need to match the keys
//quickstart(image_url).then(output => { res.end(JSON.stringify(output)) }).catch(output => { console.log('tough luck!') });
res.end('Cassia, I hope you can see this');
}
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment