I'm in the midst of making my first simple counter strike api bot with nodeJS, however I'm running into some problems with the res.send function. When I use it it throws the following error: "res.send is not a function". I've tried moving the res.send outside of the request, however that only updates my data after I refresh the page. Any help would be appreciated as I've been stuck on this problem for a while now.
My code :
const path = require('path')
const express = require('express')
var request = require("request")
/*const getSkinfo = require('./wyrskin')*/
const app = express()
const publicDirectoryPath = path.join(__dirname, '/public')
var skins_str = "empty"
app.use(express.static(publicDirectoryPath))
/*var string_skin = (JSON.stringify(getSkinfo))*/
app.get('/skin', (req, res) => {
request('https://api.steamapis.com/market/items/730?api_key=xyz', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
var skins = []
var score = 0
var i;
for (i = 0; i < body.data.length; i++) {
var name = body.data[i].market_name
var price = body.data[i].prices
var img_url = body.data[i].image
if (name.includes('Factory New') && !name.includes('StatTrak')) {
skins.push(name + "," + price.avg + "^" + img_url)
}
}
var num1 = Math.floor(Math.random() * 1215);
var num2 = Math.floor(Math.random() * 1215);
var out1 = "A : " + skins[num1]
var out2 = "B : " + skins[num2]
var mySubString1 = out1.substring(
out1.lastIndexOf(",") + 1,
out1.lastIndexOf("^")
);
var price1 = parseFloat(mySubString1)
var mySubString2 = out2.substring(
out2.lastIndexOf(",") + 1,
out2.lastIndexOf("^")
);
var price2 = parseFloat(mySubString2)
skins_str = (out1 + " ~ " + out2)
console.log(skins_str)
res.send({ skins: skins_str })
})
})
app.listen(3000, () => {
console.log('Server is up on port 3000.')
})
Thank you !
This isn't really how I would proxy a request from a node server. Checkout the node-fetch npm package. Promises really clean up the code.
But the error in your code is that you have another res variable in the scope of this function. If you rename it to response it should work?
Also use res.json() instead if you are just sending an object in your response.
Hope this helps!
app.get('/skin', (req, res) => {
request('https://api.steamapis.com/market/items/730?api_key=xyz', { json: true }, (err, response, body) => {
if (err) { return console.log(err); }
var skins = []
var score = 0
var i;
for (i = 0; i < body.data.length; i++) {
var name = body.data[i].market_name
var price = body.data[i].prices
var img_url = body.data[i].image
if (name.includes('Factory New') && !name.includes('StatTrak')) {
skins.push(name + "," + price.avg + "^" + img_url)
}
}
var num1 = Math.floor(Math.random() * 1215);
var num2 = Math.floor(Math.random() * 1215);
var out1 = "A : " + skins[num1]
var out2 = "B : " + skins[num2]
var mySubString1 = out1.substring(
out1.lastIndexOf(",") + 1,
out1.lastIndexOf("^")
);
var price1 = parseFloat(mySubString1)
var mySubString2 = out2.substring(
out2.lastIndexOf(",") + 1,
out2.lastIndexOf("^")
);
var price2 = parseFloat(mySubString2)
skins_str = (out1 + " ~ " + out2)
console.log(skins_str)
res.json({ skins: skins_str })
})
})
app.listen(3000, () => {
console.log('Server is up on port 3000.')
})
Related
This error appears constantly after running the code.enter code here.the fifth day I sit and do not understand what the problem is.if anyone understands this topic, please help.
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
"use strict";
var config = require('config');
var chalk = require('chalk');
var logError = chalk.red;
var logSuccess = chalk.green;
function padHexStr(strval, length){
var byteLength = length;
if(length === undefined || length === null || length === 0){
byteLength = 32;
}
var pad = "00".repeat(byteLength);
if(strval.substring(0,2) === "0x"){
var trunStrVal = strval.substring(2,strval.length);
return padHexStr(trunStrVal, length);
}
else{
return "0x"+pad.substring(0,pad.length-strval.length)+strval;
}
}
module.exports = {
webServer : null,
webServerPort : 8080,
webSocketIo : null,
webSocket: null,
initWebsocket: function(httpServer, done){
var self = this;
self.webSocketIo = require('socket.io');
self.webSocket = self.webSocketIo.listen(httpServer);
self.webSocket.on('connection', function(client){
var clientAddress = client.request.connection.remoteAddress;
var clientPort = client.request.connection.remotePort;
console.log('client connected from ' + clientAddress + ':' + clientPort);
});
done();
},
init : function(expressApp,done){
var self = this;
self.webServer = require("http").createServer(expressApp);
console.log('activating web server');
if(config.has('webserver.port')){
self.webServerPort = config.get('webserver.port');
}
require('./webtemplate')(expressApp);
require('./webstatic')(expressApp);
self.webServer.listen(self.webServerPort, function(err){
if(err){
logError(err);
return;
}
console.log(logSuccess("server listening on port: "+self.webServerPort));
self.initWebsocket(self.webServer,done);
});
}
};
Hey guys i am doing a message system all is working fine but now i want to add the thing that if the server crash and restart the message that have been send during this time will be send at the restart of the server. I am trying to save the information of the message in the client side and make a "waiting" system that will wait to a server response. So i wanted to know how can i do that "waiting" system because now i am doing like that :
while (socket.connected === false) {
}
But that make bug the client because the infinit loop is way too fast ... so is that possible to set a timer ? (i have already tried but i dind't find how to make a good timer in a loop).
Or maybe i am totaly wrong and i haven't to do a waiting system but an other thing so tell me if my technic will not work or if there is one better :)
So here is my code :
Client.js (startTchat is called when someone is connected)
(function($){
var socket = io.connect('http://localhost:1337');
var lastmsg = [];
var me_id = [];
var friend_ = [];
var conv_ = [];
var isPlace_ = [];
var isLocation_ = [];
var me_ = [];
var my_id;
startTchat = function(user_id, username, friend_id, conv_id, isPlace, isLocalisation) {
my_id = user_id;
socket.emit('login_chat', {
id : user_id,
username : username,
friend : friend_id,
conv : conv_id,
isPlace : isPlace,
isLocalisation : isLocalisation,
})
};
/**
* Error
*/
socket.on('error', function(err){
alert(err);
});
/**
* Messages
*/
$('#chat_form').submit(function(event){
var a = 0;
while (socket.connected === false) {
}
event.preventDefault();
console.log('ME', my_id, 'TAB', me_id);
socket.emit('new_msg', {message: $('#message').val() }, me_id[my_id], friend_[my_id], conv_[my_id], isPlace_[my_id], isLocation_[my_id], me_[my_id]);
if (a === 1) {
console.log('HEYYYYYYYYYY', my_id);
}
$('#message').val('');
$('#message').focus();
});
socket.on('new_msg', function(message, me, id_receiver, id_transmiter){
if (me.id === id_receiver || me.id === id_transmiter) {
if (lastmsg != message.user.id) {
$('#new_message').append('<span class="time_date"> ' + message.h + ' : ' + message.m + ' | ' + message.y + '-' + message.m + '-' + message.d + ' | ' + message.user.username + '</span>'
+ '<p>' + message.message + '</p>\n'
);
lastmsg = message.user.id;
} else {
$('#new_message').append('<p>' + message.message + '</p>'
);
}
}
});
/**
* Login
*/
socket.on('new_user', function(user, friend, conv, isPlace, isLocation){
me_id[user.id] = user.id;
friend_[user.id] = friend;
conv_[user.id] = conv;
isPlace_[user.id] = isPlace;
me_[user.id] = user;
isLocation_[user.id] = isLocation;
$('#new_user').append('<div class="chat_list active_chat" id="' + user.id + '">\n' +
' <div class="chat_people">\n' +
' <div class="chat_img"> <img src="https://ptetutorials.com/images/user-profile.png" alt="sunil"> </div>\n' +
' <div class="chat_ib">\n' +
' <h5>' + user.username + ' <span class="chat_date">Id : ' + user.id + '</span></h5>\n' +
' </div>\n' +
' </div>\n' +
' </div>');
});
/**
* Disconnect
*/
socket.on('disc_user', function(user){
$('#' + user.id).remove();
})
})(jQuery);
And server.js :
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'msg';
MongoClient.connect(url, function(err, client) {
if (err)
throw err;
console.log('MongoDB connected ...');
httpServer = http.createServer(function(req, res) {
console.log('This is a test');
res.end('Hello World');
});
httpServer.listen(1337);
var io = require('socket.io').listen(httpServer);
var users = {};
var messages = [];
io.sockets.on('connection', function (socket) {
const collection = client.db(dbName).collection('MessageUser');
var me = false;
var friend = false;
var conv = false;
var isPlace = false;
var room = false;
var isLocalisation = false;
for(var k in users) {
socket.emit('new_user', users[k]);
}
/**
* Login
*/
socket.on('login_chat', function (user) {
me = user;
friend = user.friend;
isPlace = user.isPlace;
conv = user.conv;
isLocalisation = user.isLocalisation;
if (isPlace === 0) {
room = user.conv;
} else {
room = user.conv + '-Place';
}
socket.join(room);
//console.log('New user : ', me.username, ' - id : ', me.id);
users[me.id] = me;
io.sockets.emit('new_user', me, friend, conv, isPlace, isLocalisation);
});
/**
* Disconnect
*/
socket.on('disconnect', function() {
if (!me) {
return false;
}
delete users[me.id];
io.sockets.emit('disc_user', me);
});
/**
* Message receive
*/
socket.on('new_msg', function(message, me_id, friend_, conv_, isPlace_, isLocalisation_, me_){
if (message.message !== '') {
message.user = me;
date = new Date();
message.h = date.getHours();
message.m = date.getMinutes();
message.y = date.getFullYear();
message.m = date.getMonth();
message.d = date.getDate();
console.log(message);
messages.push(message);
msg = {};
msg.content = message.message;
msg.sendAt = new Date();
msg.idTransmitter = me.id;
if (isPlace === 0) {
msg.idReceiver = friend;
} else {
msg.idReceiver = conv;
}
msg.idConversation = conv;
msg.isPlace = isPlace;
msg.isLocalisation = isLocalisation;
collection.insertOne(msg);
console.log('---1---', msg.idReceiver, '---2---', msg.idTransmitter, '---3---', me);
io.to(room).emit('new_msg', message, me, msg.idReceiver, msg.idTransmitter);
}
});
});
});
ps : Tell me if you need more info, sorry if i forget something that my first time using js, node and socket.io :)
while (socket.connected === false) {
}
Don't do that, it will block your page and hold your processor to 100%.
Instead, use setTimeout. It's the equivalent of sleep in javascript. You need to refactor your code to call setTimeout in a recursive fashion, and count the number of "retries" (if you want to stop at some point).
Code:
$('#chat_form').submit(function(event){
var retries = 0, max_retries = 10;
function tryNewMessage() {
if (socket.connected === false) {
if (retries >= max_retries) return; //handle max_retries properly in your code
//this is where you sleep for 1 second, waiting for the server to come online
setTimeout(tryNewMessage, 1000);
retries++;
}
else {
var a = 0;
event.preventDefault();
console.log('ME', my_id, 'TAB', me_id);
socket.emit('new_msg', {message: $('#message').val() }, me_id[my_id], friend_[my_id], conv_[my_id], isPlace_[my_id], isLocation_[my_id], me_[my_id]);
if (a === 1) {
console.log('HEYYYYYYYYYY', my_id);
}
$('#message').val('');
$('#message').focus();
}
}
tryNewMessage();
});
I want to create an application that allows a user to join multiple video call rooms in kurento.
I wrote the following code:
function joinRooms() {
var userId = document.getElementById('expertid').value;
console.log("UserID = " + userId);
var roomIds = document.getElementById('roomids').value.split(",");//Assume that we have 2 rooms r1 & r2
var userIdAlias = [];
for (var i = 0; i < roomIds.length; i++) {
userIdAlias[i] = userId + "(" + i + ")";
console.log("User alias = " + userIdAlias[i]);
}
var wsUri = 'wss://' + location.host + '/room';
kurento = KurentoRoom(wsUri, function(error, kurento) {
if (error)
return console.log(error);
for (let i = 0; i < roomIds.length; i++) {
//console.log("Start loop Roomid = " + roomId);
rooms[i] = kurento.Room({
room : roomIds[i],
user : userIdAlias[i],
subscribeToStreams : true
});
localStreams[i] = kurento.Stream(rooms[i], {
audio : true,
video : true,
data : true
});
localStreams[i].addEventListener("access-accepted", function() {
var playVideo = function(stream) {
var participantId = stream.getParticipant().getID();
var userId = document.getElementById('expertid').value;
console.log("userId = " + userId + ", participantId = " + participantId);
if (!userIdAlias.includes(participantId)) {
var elementId = "video-" + stream.getGlobalID();
var div = document.createElement('div');
div.setAttribute("id", elementId);
document.getElementById("participants").appendChild(div);
stream.playThumbnail(elementId);
// Check color
var videoTag = document.getElementById("native-" + elementId);
var userId = stream.getGlobalID();
var canvas = document.createElement('CANVAS');
checkColor(videoTag, canvas, userId);
}
}
rooms[i].addEventListener("room-connected", function(){
var j = i;
return function(roomEvent) {
document.getElementById('room-header').innerText = 'ROOM \"'
+ roomIds[j] + '\"';
//document.getElementById('join').style.display = 'none';
document.getElementById('room').style.display = 'block';
localStreams[j].publish();
var streams = roomEvent.streams;
for (var streamsIdx = 0; streamsIdx < streams.length; streamsIdx++) {
playVideo(streams[streamsIdx]);
}
}
});
rooms[i].addEventListener("stream-added", function(streamEvent) {
playVideo(streamEvent.stream);
});
rooms[i].addEventListener("stream-removed", function(streamEvent) {
var element = document.getElementById("video-"
+ streamEvent.stream.getGlobalID());
if (element !== undefined) {
element.parentNode.removeChild(element);
}
});
playVideo(localStreams[i]);
rooms[i].connect();
});
localStreams[i].init();
}
});
}
However, the code does not work. The log in server said that there is only one user request to join room r2 but not r1.
I think the reason is Javascript works with too many asynchronous task so it worked incorrectly.
Can you give some advice about the way to joining multiple rooms in kurento?
i tried to calculate root mean square using node.js, trying to be smart to make some kind of high resolution realtime measurement of stuff from serial port.
the code doesnt really matter. but each time i try to run this multiple times but it output totally different rms output value, sometimes very big, sometimes very small, NaN, etc.
the question is:
why? how? is there any node.js quirk i miss? like inter-process shared memory allocation?
it's fixed after i rewrite the program(some edit) or waiting for some time
re running a program should do the same thing everytime, right?
btw i'm using nodeserial for serial communication, at 500k baud
actual code may be posted later if you want
edit: the code
var math = require('mathjs');
var fs = require('fs');
// serving the static html "client"
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
// socket.io
var app = require('express')();
var http2 = require('http').Server(app);
var io = require('socket.io')(http2);
io.on('connection', function(socket) {
console.log('client connected');
});
/* CONSOLE INTERFACE, USED TO MANUALLY INSERT SOME KIND OF VARIABLES*/
//stdin interaction
var stdin = process.openStdin();
var command = '';
var commandmode = false;
var bufferdata;
var command = 0;
//serial stuff
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
//portName = process.argv[2];
portName = "COM4"
//parsing commandline argument as comport name
var myPort = new SerialPort(portName, {
baudRate: 500000, // choose between: 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, or 50
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\n")
});
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
var datacount = 0;
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.options.baudRate);
}
var timestamp;
var timestampold;
var dt;
function pad(num, size) {
var s = "0000" + num;
return s.substring(s.length - size);
}
function padd(num, size) {
var s = "00000000" + num;
return s.substring(s.length - size);
}
var vrms = [];
var irms = [];
var torq = [];
var speed = [];
var vraw = [];
var iraw = [];
function sendSerialData(data) {
if (datacount === 0) {
timestampold = Date.now();
}
if (datacount === 10) {
console.log(data);
}
if (datacount === 1000) {
timestamp = Date.now();
dt = timestamp - timestampold;
var samplerate;
var datalength = data.length + 1;
samplerate = datacount * 1000 / dt;
var troughput = datalength * samplerate;
var troughputp = 800 * troughput / 500000;
console.log(datacount + " data has been recorded. time elapsed:" + dt + ", samplerate:" + samplerate + " troughput:" + troughput + "/" + troughputp + "%");
}
var parseddata = data.split(",");
vraw.push(parseInt(parseddata[0], 16));
iraw.push(parseInt(parseddata[1], 16));
torq.push(parseInt(parseddata[2], 16));
speed.push(parseInt(parseddata[3], 16));
rmsfast(vraw[datacount], iraw[datacount]);
//var rawlog = padd(datacount,8) + "," + pad(vraw[datacount],4) + ","+ pad(iraw[datacount],4) +","+ pad(speed[datacount],4)+","+ pad(torq[datacount],4);
//var rawlog = padd(datacount, 8) + "," + vraw[datacount] + "," + iraw[datacount] + "," + speed[datacount] + "," + torq[datacount] + ">>" + vrms[datacount] + "," + irms[datacount];
//fs.appendFileSync('raw.txt', rawlog);
//console.log(rawlog);
++datacount;
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
var varray = [0];
// zero setup;
var mid = 2048; //ideal; changed on zerocross detection
/* the data from microcontroller is as such: VVV,III,TTT,NNN
CC = COMMAND/RUNNING MODE
VVVV = VOLTAGE READING
IIII = CURRENT READING
TTTT = TORQUE READING
NNNN = SPEED (RPM) READING
Z = IS ZERO(0=false,1=voltage,2=current)
delta t between data (microsecond);
*/
var bufferV = [0];
var bufferI = [0];
var datacount;
var dataindex; // index of data called on the below stuff
var rmslength = 80;
var sigmaVsquared = 0;
var sigmaIsquared = 0;
var lastVvalue = 0;
var lastIvalue = 0;
var Vrms = 0;
var avgV = 0;
var avgI = 0;
function rmsfast(newVoltage, newCurrent) { // example use = rmsfast(data,buffer,)
var newSqVoltage = Math.pow(newVoltage, 2);
var newSqCurrent = Math.pow(newCurrent, 2);
bufferV.push(newSqVoltage);
if (bufferV.length === 80) {
lastVvalue = bufferV.shift();
} else {
//lastVvalue = newSqVoltage;
}
avgV += ((newSqVoltage - lastVvalue) / 80);
Vrms = math.sqrt(avgV);
bufferI.push(newSqCurrent);
lastIvalue = bufferI.shift();
avgI += (newSqCurrent - lastIvalue) / 80;
var Irms = math.sqrt(avgI);
var darn = math.sqrt(4000000);
if (datacount < 1000) {
var darz = Number.isInteger(newSqVoltage);
var vavg = avgV;
var vrmss = vrms;
console.log(newVoltage + "\t---" + avgV + "\t---" + Vrms);
}
if (datacount === 1000) {
myPort.close(process.exit());
}
vrms.push(Vrms);
irms.push(Irms);
}
server.listen(2000); // listening to the port
stdin.addListener("data", function(cons) { //"listening" to stdin
command = cons.toString().trim();
});
Jade file:
form(method="post", action="/upload", enctype="multipart/form-data")
input(type="file", name="logName")
input#promptNumErrors(type="number", name="numErr", placeholder="Number of Errors")
button(type="submit") Upload
Index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res) {
res.render('fileUpload', { title: 'Log File Viewer'});
});
var formidable = require('formidable'),
fs = require('fs'),
util = require('util'); //Used to print out everything about the log file
/* POST the file upload */
router.post('/upload', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
fs.readFile(files.logName.path, function (err, data) {
if (err) throw err;
// TODO: Getting form value
var numError = req.body.numErr;
console.log(numError);
var reDiaBtoa = /^(\[[^\]]*\]) (\w+?) (\[[^\]]*\]) (\([^)]*\)) (.*)$/gm;
var outputToDisplay = " ";
// Check to see if regex string works
if (reDiaBtoa.test(data)) {
var array = data.toString().split('\n');
for (l in array) {
var logArray = (array[l]);
}
// Use numPrompt to retrieve user input for numErr
var numPrompt = 10;
// Reverse the array so the most recent errors will show
var newArray = array.reverse();
var match;
//Count the number of errors in the log file
var countErrors=0;
var numCount = (countErrors-numPrompt);
for (var j = -1; j <= newArray.length; j++) { // Since newArray[0] contains an empty string
while ((match = reDiaBtoa.exec(newArray[j])) != null) {
if (match.index === reDiaBtoa.lastIndex) {
reDiaBtoa.lastIndex++;
}
if (match[2] === "ERROR") {
countErrors++;
res.write('<p>' + "Time " + match[1] + '<br/>');
res.write("Thread Name: " + match[3] + '<br/>');
res.write("Source name & line number: " + match[4] + '<br/>');
res.write("Log Message: " + match[5] + '<br/>');
res.write('--------------------------------------' + '</p>');
}
}
}
console.log(countErrors);
}
})
});
});
It keeps saying that numError is undefined whenever I run through the program. I am able to read the file input, but not the number input. Any ideas?
UPDATE: This is the full index.js file.