After I updated to 6.9.2 from 4.4.0, my node.js file is crashing on client disconnect with something like:
this.ee._events.hasOwnProperty undefined at ultron\index.js:80 at process._tickCallback (internal/process/next_tick.js:98:9)
If I rollback to 4.4.0, it works correctly again. I am running this from nodemon server on Windows OS.
var server = require('http').createServer();
var io = require('socket.io')(server);
var http = require('http');
var querystring = require('qs');
io.on('connection', function(client){
var host = client.request.headers.host.split(':');
client.host = host[0];
console.log('new connection');
client.on('call', function(socketData){
if(socketData == "undefined" || !socketData.controller) {
return;
}
var path = stripHostFromPath(client.host, socketData.controller);
requestData(client.host, path, client.request.headers.cookie, 'GET',
function (body) {
var controllerSlices = socketData.controller.split('/');
var controller = controllerSlices[controllerSlices.length -1].replace('?', "");
var controllerObj = querystring.parse(controller);
var data = IsJsonString(body);//return json if true
if (controllerObj && controllerObj.f == 'updateUser') {
if (data != false) {
client.user_id = data.userId;
io.sockets.sockets['user_id'] = client.user_id;
}
}
if (data != false) {
if (data.broadcast) {
for (var i=0;i<data.broadcast.length;i++) {
//check if the user is connected
for (var k=0;k<io.sockets.sockets.length;k++) {
if (io.sockets.sockets[k]['user_id'] == data.broadcast[i]) {
io.sockets.sockets[k].emit(data.broadcastEvent, data.broadcastData);
}
}
}
}
}
});
});
client.on('disconnect', function(){
console.log('client disconected');
});
});
console.log('server listed on port 3000');
server.listen(3000);
function requestData(host, path, cookie, method, callbacks, postData) {
if (!method) method = 'POST';
if (!cookie) cookie = '';
var options = {
host: host,
port: '80',
path: path,
method: method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': cookie
}
};
if (postData) {
postData = querystring.stringify( JSON.parse(postData) );
options.headers['Content-Length'] = Buffer.byteLength(postData);
}
try {
var req = http.request(options, function (res) {
// response is here
//console.log(res.statusCode);
//check if request was ok.
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
callbacks(body);
});
res.on('error', function(e){
console.log(e);
});
});
if (postData) {
req.write(postData);
}
req.end();
}
catch(e) {
console.log('requestData error:');
console.log(e);
}
}
It sounds like you have an older version of socket.io installed. By upgrading to the latest version, that should update engine.io which will update ws which will update its ultron dependency to 1.0.2, where the issue is fixed.
Related
I am trying to make a HTTP get request, but nothing is being returned as text. Here is my code
const http = require('http');
const ws = require('ws');
const wss = new ws.Server({noServer: true});
var XMLHttpRequest = require('xhr2');
function accept(req, res) {
console.log("got request")
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), function(ws) {
//party name, sockets on party, limit 2
ws.on('message', function (msg) {
var message = JSON.parse(msg.toString())
if(message.type==='add') {
//do later
} else if(message.type==='start') {
httpGetAsync('192.168.26.23', "/startRelay")
} else if(message.type==='stop') {
httpGetAsync('92.168.26.23', "/stopRelay")
} else if(message.type==='status') {
httpGetAsync('92.168.26.23', "/")
}
});
ws.on('close', function () {
console.log('closed')
});
ws.on('error', function(error){
console.log('error - ' + JSON.stringify(error));
});
})
}
function httpGetAsync(theUrl, path) {
var options = {
host: theUrl,
path: path,
method: "GET",
}
callback = async function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(JSON.parse(str));
});
};
http.request(options, callback).end();
}
http.createServer(accept).listen(3000);
Here is what 192.168.26.23 has in it (if you type it up from my wifi onto google, this is what shows - and this data is what I need to get in a string)
{"success":true,"ms_left":0,"relayStarted":false}
So we are supposed to set up an WebServer with Fuseki and Nodejs.
I defined the classes with the following OWL Ontology and loaded it up to the Fuseki server.
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:ns0="http://localhost:3000/data#">
<owl:DatatypeProperty rdf:about="http://localhost:3000/data#message">
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://localhost:3000/data#firstname">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://localhost:3000/data#lastname">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
</owl:DatatypeProperty>
<owl:Class rdf:about="http://localhost:3000/data#linkeddata">
<rdfs:comment>The class of all data types.</rdfs:comment>
<rdfs:label>The data type</rdfs:label>
</owl:Class>
<owl:Class rdf:about="http://localhost:3000/data#users">
<rdfs:comment>Users that post Tweets.</rdfs:comment>
<rdfs:label>Users</rdfs:label>
<rdfs:subClassOf rdf:resource="http://localhost:3000/data#linkeddata"/>
</owl:Class>
<owl:Class rdf:about="http://localhost:3000/data#tweets">
<rdfs:comment>Tweets contain messages of Users.</rdfs:comment>
<rdfs:label>Tweets</rdfs:label>
<rdfs:subClassOf rdf:resource="http://localhost:3000/data#linkeddata"/>
</owl:Class>
<owl:ObjectProperty rdf:about="http://localhost:3000/data#tweetedby">
</owl:ObjectProperty>
<rdf:Description rdf:about="http://localhost:3000/data#1234">
<rdf:type rdf:resource="http://localhost:3000/data#tweets"/>
<ns0:message>blablubbtweet</ns0:message>
<ns0:tweetedby>
<ns0:users rdf:about="http://localhost:3000/data#2345">
<ns0:firstname>Manuel</ns0:firstname>
<ns0:lastname>Neuer</ns0:lastname>
</ns0:users>
</ns0:tweetedby>
</rdf:Description>
</rdf:RDF>
I hope you can read it. It works however. I can then view the classes directly by typing this into Fuseki web interface. With "'2345" for example being the User Manuel Neuer.
SELECT * WHERE { GRAPH ?g { <http://localhost:3000/data#2345> ?p ?o}}
However, in my Node.js application, I somehow am only able to get the whole graph, and I dont know why.
The following is the Nodejs code :
I think the error must be at
function sparql_GET_Data_Element (URIhash) {
postData =
` describe <http://localhost:3030/data#` + URIhash + `>`
Its like he would always describe http:/localhost:3030/data# and never take the urihash. I would be delighted about every tip. Thanks alot. I test it by sending GET requests with Insomnia Rest Api.
http://localhost:3030/data#2345 this get request generates the same whole graph as http://localhost:3030/data#users.
const express = require('express')
const app = express()
const app_port = 3000
const apache_fuseki_persistence_url = 'http://localhost:3030'
const http = require('http');
const rdf_url_prefix_tag = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>'
const rdff_url_prefix_tag = 'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>'
const SPARQL_GET_ALL_CLASSES = function (){
postData =
`
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?class ?label ?description
WHERE {
?class a owl:Class.
OPTIONAL { ?class rdfs:label ?label}
OPTIONAL { ?class rdfs:comment ?description}
}
`
const options = {
hostname: 'localhost',
port: 3030,
path: '/data/query',
method: 'POST',
headers: {
'Accept': 'application/sparql-results+json',
'Content-Type': 'application/sparql-query'
}
}
return new Promise(function(resolve, reject) {
var req = http.request( options, function(res) {
// reject on bad status
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var data = {};
res.on('data', function(chunk) {
data = chunk;
});
// resolve on end
res.on('end', function() {
resolve(JSON.parse(data));
});
});
// reject on request error
req.on('error', function(err) {
// This is not a "Second reject", just a different sort of failure
reject(err);
});
if (postData) {
req.write(postData);
}
// IMPORTANT
req.end();
});
}
function sparql_GET_Data_Element (URIhash) {
postData =
` describe <http://localhost:3030/data#` + URIhash + `>`
const options = {
hostname: 'localhost',
port: 3030,
path: '/data/query',
method: 'POST',
headers: {
'Accept': 'application/ld+json',
'Content-Type': 'application/sparql-query'
}
}
return new Promise(function(resolve, reject) {
var req = http.request( options, function(res) {
// reject on bad status
if (res.statusCode < 200 || res.statusCode >= 300) {
reject(new Error('statusCode=' + res.statusCode));
}
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
});
// resolve on end
res.on('end', function() {
resolve(JSON.parse(data[0]));
});
});
// reject on request error
req.on('error', function(err) {
// This is not a "Second reject", just a different sort of failure
reject(err);
});
if (postData) {
req.write(postData);
}
// IMPORTANT
req.end();
});
}
function sparql_CREATE_NEW_CLASS(new_rdfs_class){
}
// var promiseB = promiseA.then(function(result) {
// // do something with result
// });
function rdfs_class_exists (requested_class){
return SPARQL_GET_ALL_CLASSES().then(result => {
let found_class = false;
result.results.bindings.forEach(element => {
if (element.class.value == 'http://localhost:3000/data#'+requested_class) {
found_class = true;
}
})
return found_class;
});
}
app.get('*', function(req, res, next){
path_elements = req.path.split('#');
var requested_class = path_elements[1];
rdfs_class_exists(requested_class).then((RDFS_Class_found) => {
if (RDFS_Class_found){
if(path_elements.length > 3){
sparql_GET_Data_Element(path_elements[3]).then(result => {
console.log('result: ' + result);
res.send(result);
}).catch(error => {
console.log(error);
res.send(error);
})
} else {
sparql_GET_Data_Element(path_elements[1]).then(result => {
console.log('result: ' + result);
var ele = sparql_GET_Data_element(path_elements[1]);
res.send(ele);
// res.send(result);
}).catch(error => {
console.log(error);
res.send(error);
})
}
} else res.status(400).send("RDFS Class not Found");
});
})
app.post('*', function(req, res, next){
next();
})
app.delete('*', function(req, res, next){
next();
})
app.put('*', function(req, res, next){
next();
})
app.all('*', function (req, res, next){
res.status(400).send('No Elements found');
})
/**
* Die API wartet auf Requests am Port 3000.
* Wird sie per Node gitlokal gestartet läuft sie unter http://localhost:3000/
*/
app.listen(app_port, function () {
console.log('Example app listening on port 3000!');
});
i know this question asked many time before but still i'm struggling to figure this out. i have a set of js files. first one is index.js
app.all('/backend/*/*', function(req, res){ // backend/product/getProduct
serviceType = req.params[0];
methodType = req.params[1];
exports.serviceType= serviceType;
exports.methodType= methodType;
main.checkService()
});
in here im extracting the params and call checkService method in main.js file
main.js
function checkService(){
switch(index.serviceType){
case 'product':
product.checkMethod();
break;
default :
console.log('no such service')
}
}
then it move to product.js file
function checkMethod(){
var methodName = index.methodType,
res = index.res,
req = index.req;
switch(methodName){
case 'samplePost':
var body = req.body;
proHan.samplePost(body,function(data,msg,status){
sendRes(data,msg,status);
});
break;
default :
console.log('no such method')
}
function sendRes(jsonObj,msg,status){
var resObj = {
status : status,
result : jsonObj,
message : msg
}
res.json(resObj);
}
first it moves to samplePost method in handler.js
once the http req finised executing, callback return the results and call sendRes method and send the json
function samplePost(jsonString,cb){
var res = config.setClient('nodeSample');
// jsonString = JSON.parse(jsonString);
res.byKeyField('name').sendPost(jsonString,function(data,msg,status){
cb(data,msg,status);
});
}
to send http req i written a common file. that is config.js
function setClient(_cls){
var client = new Client(url);
return client;
}
function parentClient(url){
this.postBody = {
"Object":{},
"Parameters":{
"KeyProperty":""
}
};
}
function paramChild(){
parentClient.apply( this, arguments );
this.byKeyField = function(_key){
this.postBody.Parameters.KeyProperty = _key;
return this;
}
}
function Client(url){
parentClient.apply( this, arguments );
this.sendPost = function(_body,cb){
_body = (_body) || {};
this.postBody.Object = _body;
var options = {
host : 'www.sample.com',
port : 3300,
path: '/somrthing',
headers: {
'securityToken' : '123'
}
};
options.method = "POST";
var req = http.request(options, function(response){
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
cb(JSON.parse('[]'),'success',200)
});
});
//This is the data we are posting, it needs to be a string or a buffer
req.on('error', function(response) {
cb(JSON.parse('[]'),response.errno,response.code)
});
req.write(JSON.stringify(this.postBody));
req.end();
}
}
paramChild.prototype = new parentClient();
Client.prototype = new paramChild();
when i send the first req its work but from then again the server crashes. it seems like i can't call res.end method again in a callback method. how can i fix this. thank you.
you can't call res.end two times. Here is a simple exemple to deal with callback with a basic node server.
const http = require('http');
const hostname = '127.0.0.1';
const port = 4242;
let something = true;
function callback(req, res) {
something = !something;
res.setHeader('Content-Type', 'text/plain');
res.end('Callback Hello World\n');
}
const server = http.createServer((req, res) => {
res.statusCode = 200;
if (something) {
callback(req, res);
} else {
something = !something;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I am trying to send API calls to a NodeJS server. Unfortunately, the server (that was not made by me) won't allow me to make async calls.
I am trying to figure out a way to make every request to wait for the previous request to finish before being sent.
Here is my code:
var http = require('http');
var fs = require('fs');
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '8080',
path: '/api/scan',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var array = ["ssl.com", "google.com", "hamzakhan.org"];
for (var i=0; i<array.length;i++) {
//console.log(array[i]);
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body);
// Make sure it's working
console.log(body.response.subject);
});
});
var url = array[i];
var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
post_req.write(JSON.stringify(catURL), function(err) {
//console.log(err);
post_req.end();
});
}
I was thinking I could nest a while loop inside the for loop, and the while loop would have a flag that would dictate when the for loop could keep going.
Now, this code works when I hardcode the value of url as a single url, so i know that I am successful in sending and receiving.
Thanks for all/any help!
/**
* Disclaimer: I did not test your code. I simply added a few
* lines to illustrate my suggestion.
*/
var http = require('http');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '8080',
path: '/api/scan',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var array = ["ssl.com", "google.com", "hamzakhan.org"];
var emitter = new EventEmitter();
var counter = 0, n = array.length;
function PostRequest() {
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body);
// Make sure it's working
console.log(body.response.subject);
// ADD THE CALLBACK
// OR
// TRIGGER EVENT
return emitter.emit('ResponseEnded');
});
});
var url = array[counter];
var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
post_req.write(JSON.stringify(catURL), function(err) {
//console.log(err);
post_req.end();
});
}
emitter.on('ResponseEnded',function() {
++counter;
if (counter < n) {
PostRequest();
}
else {
console.log('Nothing more request');
}
});
// Start with the first request
PostRequest();
Update: This is another way of doing the same thing.
/**
* Disclaimer: I did not test your code. I simply added a few
* lines to illustrate my suggestion.
*/
var http = require('http');
var fs = require('fs');
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '8080',
path: '/api/scan',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var array = ["ssl.com", "google.com", "hamzakhan.org"];
var counter = 0, n = array.length;
function PostRequest() {
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body);
// Make sure it's working
console.log(body.response.subject);
// THIS IS ANOTHER WAY OF DOING IT
++counter;
if (counter < n) {
/**
* This is were your start new
* requests
*/
PostRequest();
}
else {
console.log('Nothing more request');
}
});
});
var url = array[counter];
var catURL = { "url": url, "path": "/", "port": "443", "live_scan": "false", "advanced": "true" };
post_req.write(JSON.stringify(catURL), function(err) {
//console.log(err);
post_req.end();
});
}
// Start with the first request
PostRequest();
I am working in MEAN STACK application and i want to set mail's parameters dynamic.
route.js
var Helper = require("../helper.js");
router
.route("/api/user/registration")
.POST(function (req, res) {
//after user insert send mail
Helper.sendCustomEmail(params, function (error, response) {
if (error) {
console.log("Mail : " + error);
res.json({"status": 0, "error": {"other": "Oops! something went wrong, please try again later"}});
} else {
console.log("Message sent");
res.json({status: 1, message: 'Thank you for registration. You will get verification email soon', token: res.locals.user.jwttoken});
}
});
});
Helper.js
exports.sendCustomEmail = function(params, callback) {
//Include nodejs mailer and smtp module
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
//read header email template
var headerHtml = params.fs.readFileSync(basePath + "emailHeader.html").toString();
//read footer email template
var footerHtml = params.fs.readFileSync(basePath + "emailFooter.html").toString();
//Your dynamic template html only
var dynamicMessage = params.message;
var message = headerHtml + dynamicMessage + footerHtml;
message = message.replace(/##DOMAIN_URL##/g, "http://" + params.domainUrl);
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpTransport({
host: this.getSiteSetting("SMTP_HOST"),
secure: true,
auth: {
user: this.getSiteSetting("SMTP_USER"),
pass: this.getSiteSetting("SMTP_PSSSWORD")
},
tls: {
rejectUnauthorized: false
}
}));
transporter.sendMail({
from: this.getSiteSetting("SMTP_FROM"),
to: params.to, // receiver
subject: params.subject,
html: message // body
}, function(error, response) { //callback
callback(error, response);
});
};
var SiteSetting = require('../models/siteSetting');
exports.getSiteSetting = function($keyword) {
if ($keyword !== undefined && $keyword !== null && $keyword !== "") {
SiteSetting.findOne({setting_key : $keyword},function(err,siteSetting){
if(err){
return null;
}else{
if(siteSetting !== null){
console.log(siteSetting.setting_value);
return siteSetting.setting_value;
}
}
});
}else{
return null;
}
};
dependencies
"express" => "version": "4.13.4",
"mongoose" => "version": "4.4.4",
"mongodb" => "version": "2.4.9",
"OS" => "ubuntu 14.04 lts 32bit",
from the following code SiteSetting function console.log print properly but before return mail send error occur.
Please give me a proper guideline for this code.
This can be solved as bellow.
exports.sendCustomEmail = function(params, callback) {
//Include nodejs mailer and smtp module
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
//read header email template
var headerHtml = fs.readFileSync(basePath + "app/client/views/layout/emailTemplates/emailHeader.html").toString();
//read footer email template
var footerHtml = fs.readFileSync(basePath + "app/client/views/layout/emailTemplates/emailFooter.html").toString();
//Your dynamic template html only
var dynamicMessage = params.message;
var message = headerHtml + dynamicMessage + footerHtml;
message = message.replace(/##DOMAIN_URL##/g, "http://" + params.domainUrl);
var host = null;
var user = null;
var pass = null;
var from = null;
this.getSiteSetting("SMTP_HOST", function(res) {
host = res;
setParams();
});
this.getSiteSetting("SMTP_USER", function(res) {
user = res;
setParams();
});
this.getSiteSetting("SMTP_PASS", function(res) {
pass = res;
setParams();
});
this.getSiteSetting("MAIL_FROM", function(res) {
from = res;
setParams();
});
var setParams = function() {
if (host !== null && user !== null && pass !== null && from !== null) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpTransport({
host: host,
//port: 25,
//port: 465,
secure: true,
auth: {
user: user,
pass: pass
},
tls: {
rejectUnauthorized: false
}
}));
transporter.sendMail({
from: from,
to: params.to, // receiver
subject: params.subject,
html: message // body
}, function(error, response) { //callback
callback(error, response);
});
}
};
}
var SiteSetting = require('../models/siteSetting');
exports.getSiteSetting = function(keyword, callback) {
if (keyword !== undefined && keyword !== null && keyword !== "") {
SiteSetting.findOne({ setting_key: keyword }, function(err, siteSetting) {
if (err) {
callback(null);
} else {
if (siteSetting !== null) {
//console.log(siteSetting.setting_value);
callback(siteSetting.setting_value);
}else{
callback(null);
}
}
});
} else {
callback(null);
}
};