1

We are building a server with net module, and having hard time extracting the URL (and resource path) from the request. The following code crashes, saying:

Parameter 'url' must be a string not undefined.

File netServer.js:

var net = require('net');
var url = require('url');

var server = net.createServer(function(socket) { //'connection' listener

   socket.on('connect', function(request) {
   });

  socket.on('data', function(request) {
      var pathname = url.parse(request.url).pathname;
      console.log("Request for " + pathname + " received.");

  });

  socket.on('end', function() {

  });

});
server.listen(8080, function() { //'listening' listener
    console.log('server bound');
});

Any suggestions?

0

1 Answer 1

2

Are you trying to build an HTTP server? net is a TCP package, so all you get is the remoteAddress and remotePort, the rest will be sent on the data handler (which is just passed a Buffer, or a string, depending on the encoding).

Use the HTTP module for this, because it does all of the parsing for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.