Reputation: 2841
I am making a NodeJS/Express listener and have inputs which are being created as my connectListener
is being made:
app.get('/server/:args', function(req, res) {
res.sendFile(__dirname + '/index.html');
// ...
var args = req.param("args");
var client = net.connect({port : 50505}, connectListener(args, client));
});
Then I have my connectListener
function:
function connectListener(args, client) {
console.log("Connected to server.");
console.log(args);
client.write(args);
// ....
}
However, I get an error that the arguments are undefined:
TypeError: Cannot read property 'write' of undefined
at connectListener (C:\Users\ES\workspace_luna\WptHash\server-src\index.js:39:8)
at C:\Users\ES\workspace_luna\WptHash\server-src\index.js:33:43
at Layer.handle [as handle_request] (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\layer.js:82:5)
at next (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\route.js:100:13)
at Route.dispatch (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\route.js:81:3)
at Layer.handle [as handle_request] (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\layer.js:82:5)
at C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\index.js:235:24
at param (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\index.js:332:14)
at param (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\index.js:348:14)
at Function.proto.process_params (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\lib\router\index.js:392:3)
Error: Can't set headers after they are sent.
at SendStream.headersAlreadySent (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\node_modules\send\index.js:313:13)
at SendStream.send (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\node_modules\send\index.js:494:17)
at onstat (C:\Users\ES\workspace_luna\WptHash\server-src\node_modules\express\node_modules\send\index.js:585:10)
at FSReqWrap.oncomplete (fs.js:99:15)
So, how do I properly create the client
variable such that it can be used in the below function? The reason I am making the function connectListener
like this instead of inline is I plan to replicate the client across more ports to handle simultaneous connections so I don't want to re-use code nor use a global set of client variables/array (though I suppose I could...)
Upvotes: 1
Views: 86
Reputation: 4975
Looks like the problem is you are calling connectListener
where you should just be setting it as the function to be called on a connect event:
var client = net.connect({port : 50505}, connectListener);
If you want to write to a specific client with specific args, you can do so by defining the response in your connectListener function:
app.get('/server/:args', function (req, res) {
var client, args;
res.sendFile(__dirname + '/index.html');
// ...
function connectListener () {
console.log("Connected to server.");
console.log(args);
client.write(args);
// ....
}
args = req.param("args");
client = net.connect({port : 50505}, connectListener);
});
Upvotes: 1