AJAX 404 with Node and Koa - javascript

Browser JS
'use strict';
window.onload = () => {
let form = document.getElementById('sign_up_form'),
username = form.elements[0],
password = form.elements[1],
confirm = form.elements[2],
email = form.elements[3],
errors = document.getElementById('sign_up_errors');
username.addEventListener('change', (e) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', '/validate_username');
xhr.send();
xhr.onreadystatechange = () => {
console.log(xhr.readyState);
if (xhr.readyState === 4) {
console.log(xhr.status);
if (xhr.status === 200) {
console.log('AJAX SUCCESS');
};
};
};
});
confirm.addEventListener('change', (e) => {
if (password.value != confirm.value) {
errors.children[1].innerHTML = 'Passwords do not match.';
} else {
errors.children[1].innerHTML = '';
};
});
form.addEventListener('submit', (e) => {
e.preventDefault();
let form_data = {
username: username.value,
password: password.value,
confirm: confirm.value,
email: email.value,
};
let xhr = new XMLHttpRequest();
xhr.open('POST', '/validate_signup');
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(form_data));
});
}
Server
'use strict';
let app = require('koa')(),
serve = require('koa-static'),
router = require('koa-router')(),
parse = require('koa-bodyparser'),
mongo = require('koa-mongo'),
fs = require('co-fs');
app.use(serve(__dirname + '/public'));
app.use(mongo({
uri: ******,
max: 100,
min: 1,
timeout: 30000,
log: false
}));
app.use(parse());
app.use(router.routes());
router.post('/validate_username', function *(next) {
console.log('username:');
console.log(this.request.body);
});
router.post('/validate_signup', function *(next) {
console.log('signup:');
console.log(this.request.body);
this.mongo.collection('users').findOne({'username': this.request.body.username}, (err, doc) => {
console.log(doc);
});
});
app.listen(5000);
The AJAX 'POST' request gives the form_data to the server and I can check the database but consoles 404 error. The AJAX 'GET' request just throws a 404 error after achieving readyState 4. I think I am using the routes incorrectly or am missing something in my AJAX requests but I am new to Koa.js and pretty much green all around so any help will be appreciated.

JS:
'use strict';
window.onload = () => {
let form = document.getElementById('sign_up_form'),
username = form.elements[0],
password = form.elements[1],
confirm = form.elements[2],
email = form.elements[3],
errors = document.getElementById('sign_up_errors');
username.addEventListener('input', (e) => {
let data = {username: username.value};
let xhr = new XMLHttpRequest();
xhr.open('POST', '/sign_up/username');
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = () => {
console.log(xhr.readyState);
if (xhr.readyState === 4 && xhr.status === 200) {
if (JSON.parse(xhr.responseText).username) {
console.log('unavailable');
} else {
console.log('available');
};
};
};
});
};
SERVER:
'use strict';
let router = require('koa-router')({
prefix: '/sign_up'
});
router.post('/username', function *(next) {
console.log('Checking username...');
yield new Promise((resolve, reject) => {
this.mongo.collection('users').findOne({'username': this.request.body.username}, (err, doc) => {
if (doc) {resolve(doc)}
else {reject()};
});
}).then((doc) => {
if (doc) {
console.log('Promise: ' + doc);
this.body = {username: false};
};
}).catch(() => {
console.log('Promise rejected.');
this.body = {username: true};
});
});
module.exports = router;

Related

Getting returned value from another javascript file using async

class monitor {
constructor(){
this.delay = config.delay
delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
async redacted (pid) {
if (this.err === true) {
await this.delay(this.delay)
}
console.log("MONITOR > Getting Item Attrs")
const options = {
method: 'get',
url: url + pid + '.json',
headers: {
accept: '*/*',
'accept-encoding': 'gzip, deflate, br',
},
proxy: this.proxy
}
return req(options)
.then((res) => {
//console.log(res)
let variants = res.data.skus
//console.log(variants)
const att = []
for (let [key, value] of Object.entries(variants)) {
if(value.inStock) att.push(key)
}
if(att.length >= 1){
console("MONITOR > Sourced Item")
return att;
} else {
("MONITOR > No Variants Available")
this.oos = true
this.redacted(config.pid);
}
})
.catch((err) => {
if (err?.response?.status == 403) {
console.error("MONITOR > Proxy Block # GET PRODUCT")
this.err = true
this.redacted(config.pid);
}
})
}
}
var hello = new monitor().redacted(config.pid);
console.log(hello)
From what I understand I need to wait for the promise to finish before returning but I am confused on how to execute this with my code and also call this file and store the return value in another file. I'm not sure if my formatting is wrong as well, but I tried changing and no fix anyways
This snippet isn't using asynch but, it gives you the idea. You need to await or .then the promise (the fetch/request). You first fetch the remote JSON file then you parse it / read it. Something like:
function getJSON(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
//xhr.responseType = 'json';
xhr.onload = function () {
var status = xhr.status;
if (status == 200) {
resolve(JSON.parse(xhr.response)); // <---------------
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON(primaryURL).then(function (res) {
if (res) {
//do something
} else {
console.log("response not found");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});

Recover public address from a typed signature

I am implementing an application, on which it is necessary to confirm your Ethereum wallet. In order to do so, I am currently writing a basic HTML and Javascript Web page.
This is my javascript code.
const msgParams = [
{
type: 'uint',
name: 'Please verify your generated key',
value: ''
}
]
var signeddata = ''
function sanitizeData (data) {
const sanitizedData = {}
for (const key in TYPED_MESSAGE_SCHEMA.properties) {
data[key] && (sanitizedData[key] = data[key])
}
return sanitizedData
}
window.onload = function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://plapla.pla/initializeVerification', true);
// If specified, responseType must be empty string or "text"
xhr.responseType = 'json';
xhr.onreadystatechange = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
msgParams[0].value = xhr.response.key;
console.log(msgParams);
}
}
};
console.log('!');
xhr.send(null);
}
function verify() {
let web3 = window.web3;
console.log(web3);
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use the browser's ethereum provider
web3 = new Web3(web3.currentProvider);
console.log(web3);
} else {
console.log('No web3? You should consider trying MetaMask!')
}
//Login tracken
web3.currentProvider.publicConfigStore.on('update', callback => {
console.log(callback);
//Login tracken
});
console.log(web3.eth.accounts);
web3.eth.getCoinbase(function(error, result){
if(!error) {
console.log("params: "+msgParams[0]);
var fromAddress = result;
web3.currentProvider.sendAsync({
method: 'eth_signTypedData',
params: [msgParams, fromAddress],
from: fromAddress,
}, function (err, result) {
if (err) return console.error(err);
if (result.error) {
return console.error(result.error.message)
}
var sign = {};
sign.data =[{
type:msgParams[0].type,
name:msgParams[0].name,
value:msgParams[0].value
}];
sign.sig = result.result
var json = JSON.stringify(sign);
console.log("Do JSON"+json);
var xhr = new XMLHttpRequest();
console.log("Fa: "+fromAddress);
xhr.open('POST', 'https://plapla.pla/addWallet', true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
// If specified, responseType must be empty string or "text"
xhr.responseType = 'text';
xhr.onreadystatechange = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
console.log(xhr.response);
}
}
};
xhr.send(json);
});
}
});
};
I am retrieving a random number from my backend on load and want the User to sign this Code with Metamask. I then send it again to my firebase backend, which receives the data as well as the signature.
Firebase handles it as Follows:
exports.addWallet = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const signed = req.body;
console.log(signed);
const recovered = sigUtil.recoverTypedSignature(signed);
return recovered;
})
});
As you can see, I am using the eth-sig-util library: https://github.com/MetaMask/eth-sig-util
But I always get this error from firebase:
TypeError: Cannot read property 'EIP712Domain' of undefined
at Object.findTypeDependencies (/user_code/node_modules/eth-sig-util/index.js:97:47)
at Object.encodeType (/user_code/node_modules/eth-sig-util/index.js:76:21)
at Object.hashType (/user_code/node_modules/eth-sig-util/index.js:127:30)
at Object.encodeData (/user_code/node_modules/eth-sig-util/index.js:42:33)
at Object.hashStruct (/user_code/node_modules/eth-sig-util/index.js:116:30)
at Object.sign (/user_code/node_modules/eth-sig-util/index.js:153:21)
at Object.recoverTypedSignature (/user_code/node_modules/eth-sig-util/index.js:235:36)
at cors (/user_code/index.js:29:31)
at cors (/user_code/node_modules/cors/lib/index.js:188:7)
at /user_code/node_modules/cors/lib/index.js:224:17
So I figured out, that the problem is with the library... Do I send the wrong parameters to the function? Is there any other way to recover the public address from the signer?
You need to use object data, can check code here:
https://github.com/MetaMask/eth-sig-util/blob/master/index.js#L234
{
data: '', // the data you signed
sig: '' // the r, s, v concated string
}
Or you can use ethereumjs-util to recover the public key if you know the signed data.

javascript promise callback

I am calling a javascript function , which in turn calls a web service;The response of this service is used to call another function which also calls a service. At end of both services we set session attributes. This code gives no errors, but the callback gets called before the service has returned data. The main motive of this code is to set the session attributes before return of flow from this code, when the callback gets called before the service has returned values the session attributes are not set and the requirement of the code is not fulfilled.
'use strict';
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message : 'For security purpose answer these questions '
},
};
}
function getSecurityQuestions(intentRequest, context, post_options, callback){
const sessionAttributes = intentRequest.sessionAttributes || {};
var policynumber = sessionAttributes.policynumber;
var interactionID = sessionAttributes.interactionID;
var body = "";
var body2;
const http = require('https');
const promise = new Promise((resolve, reject) => {
const post_data = JSON.stringify({"Purpose":"SecurityQuestions", "InteractionID":interactionID, "SearchStringAcctNum":policynumber});
//ignores SSL
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var post_request = http.request(post_options, function(res) {
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
context.done(body);
resolve(body);
});
res.on('error', function(e) {
reject(Error(e.message));
context.fail('error:' + e.message);
});
});
// post the data
post_request.write(post_data);
post_request.end();
});
callback( promise.then((body) => {
body2 = JSON.parse(body);
sessionAttributes.question1 = body2.SecurityDetails[0].Question;
close(sessionAttributes, 'Fulfilled');
}, (error) => {
console.log(error.message);
})
);
}
function getInteraction(intentRequest, context, callback) {
const slots = intentRequest.currentIntent.slots;
var policynumber = "PA"+slots.PolicyNumber;
var questionOne = slots.questionOne;
var questionTwo = slots.questionTwo;
const sessionAttributes = intentRequest.sessionAttributes || {};
console.log("policy number : "+policynumber + "question 1 : "+questionOne + "question 2 : "+questionTwo);
sessionAttributes.policynumber = policynumber;
var body = "";
var body2;
// An object of options to indicate where to post to
var post_options = {
host: 'example.com',
protocol: 'https:',
port: '3000',
path: '/hiddenPath',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const http = require('https');
const promise = new Promise((resolve, reject) => {
const post_data = JSON.stringify({"Purpose":"CreateInteraction"});
//ignores SSL
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var post_request = http.request(post_options, function(res) {
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
context.done(body);
resolve(body);
});
res.on('error', function(e) {
console.log("rejected here");
reject(Error(e.message));
context.fail('error:' + e.message);
});
});
// post the data
post_request.write(post_data);
post_request.end();
});
callback( promise.then((body) => {
body2 = JSON.parse(body);
console.log("interaction ID : "+body2.InteractionID);
sessionAttributes.interactionID = body2.InteractionID;
getSecurityQuestions(intentRequest, context, post_options, callback);
}, (error) => {
console.log('Promise rejected.');
console.log(error.message);
}));
}
// --------------- Intents -----------------------
/**
* Called when the user specifies an intent for this skill.
*/
function dispatch(intentRequest, context, callback) {
const intentName = intentRequest.currentIntent.name;
if (intentName === 'currIntent') {
return getInteraction(intentRequest, context, callback);
}
throw new Error(`Intent with name ${intentName} not supported`);
}
// --------------- Main handler -----------------------
function loggingCallback(response, originalCallback) {
console.log("logging callback called......");
originalCallback(null, response);
}
exports.handler = (event, context, callback) => {
try {
dispatch(event, context, (response) => loggingCallback(response, callback));
} catch (err) {
callback(err);
}
};
You should resolve your promise only after the request ends.. Have updated your sample below. Hope it helps. Also, you were sending an invalid object as your post body. Fixed that as well.
function getValue(context, post_options, callback) {
var body = "";
var body2;
const http = require('http');
const promise = new Promise((resolve, reject) => {
// INVALID OBJECT
//const post_data = JSON.stringify({"something"});
const post_data = JSON.stringify({
something: "something"
});
//ignores SSL
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var post_request = http.request(post_options, function(res) {
res.on('data', function(chunk) {
body += chunk;
console.log("inside " + JSON.stringify(body));
// DONT RESOLVE HERE, REQUEST IS NOT COMPLETE
//resolve(body);
});
res.on('end', function() {
context.done(body);
//RESOLVE HERE INSTEAD
resolve(body);
});
res.on('error', function(e) {
reject(Error(e.message));
context.fail('error:' + e.message);
});
});
// post the data
post_request.write(post_data);
post_request.end();
});
promise.then((body) => {
console.log("response data " + JSON.stringify(body));
body2 = JSON.parse(body);
callback(delegate(sessionAttributes, intentRequest.currentIntent.slots));
}, (error) => {
console.log('Promise rejected.');
console.log(error.message);
});
}

how receive http response of binary pdf and show it. angular 2 - ionic2

I am tring to recive a pdf convert to binary(64) from a rest service, and show it in ionic 2, i try this:
I am tring to recive a pdf convert to binary(64) from a rest service, and show it in ionic 2, i try this:
in service
function(token:String, docCaseNumber: String){
this.setTokenHeaders(token);
this.headers.append('Accept', 'application/octet-stream');
let options = new RequestOptions({ headers: this.headers });
return new Promise(resolve => {
this.http.get(url, options)
.map(res => res)
.subscribe(data => {
//console.log(data);
resolve(data.text());
}, err => {
resolve("connException");
});
});
}
in component
getXX(){
this.miservice.functoin(this.token, 'H17-09601').then(
(data) => {
var blob = data;
console.log(blob);
this.base64ToUint8Array(data);
//this.getBase64(data);
var converted = new Blob([blob], {type:'application/pdf'});
this.convertToBase64(converted);
//this.base64ToUint8Array(converted);
//this.fileURL = URL.createObjectURL(converted);
//window.open(fileURL);
var pdf = pdfMake.createPdf(this.buildPdf(converted));
pdf.getBase64(function (output) {
this.base64ToUint8Array(output);
});
//console.log(pdf);
pdf.open;
//console.log(data);
});
}
here is the answer. You can use this for show pdf
Modify according to your needs ;)
return new Promise(resolve => {
let xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.setRequestHeader("Access-Control-Allow-Origin", '*');
xhr.setRequestHeader('Accept', 'application/octet-stream');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
// xhr.overrideMimeType('text/xml; charset=iso-8859-1');
xhr.overrideMimeType('text/xml; charset=iso-8859-1');
xhr.responseType = "arraybuffer";
xhr.onreadystatechange= function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(JSON.stringify((xhr.response)));
resolve(new Blob([xhr.response], { type: 'application/pdf' }));
} else {
//do something
}
}
}
xhr.send();
});
then use it like this:
your-function.then(
(blob :Blob) => {
var downloadUrl = URL.createObjectURL(blob);
var downloadLink = document.createElement('a');
downloadLink.href = downloadUrl;
downloadLink.target = '_blank';
document.body.appendChild(downloadLink);
downloadLink.click();
});

HttpRequest Node.js "Cannot set property 'UNSENT' of undefined"

I have a problem, when I try to run a simple http request I always get an error message.
Cannot set property 'UNSENT' of undefined
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
XMLHttpRequest('https://example.firebaseio.com/.json',
function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
}
);
How do I fix this problem?
I suggest you use the standard built-in NodeJS methods:
This code:
var http = require('http');
http.get('http://nodejs.org/dist/index.json', (res) => {
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error(`Request Failed.\n` +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\n` +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
let parsedData = JSON.parse(rawData);
console.log(parsedData);
} catch (e) {
console.log(e.message);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
is taken from here
This will work.
var req = new XMLHttpRequest()
req.open('GET',url)
req.onload = function(){
if(req.status==200){
console.log(req.responseText)
}
else{
console.log(req.statusText)
}
}
req.onerror = function()
{
console.log('Network Error')
}
req.send()
})

Categories

Resources