php - How to set api routes in a nodejs udp server?

Is it possible to set different routes on an udp server like on a http server?
I want to implement a simple nodejs server to call it in php like this.
"udp://" . $host . "/api/" . $apiVersion . "/myEndpoint";
Itried this
var PORT = 1337;
var HOST = '127.0.0.1/api/v1/somethig';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function() {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ':' + address.port);
});
server.on('message', function(message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
server.bind(PORT, HOST);
with the route in HOST I got the folowing error.
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 127.0.0.1/api/v1/somethig
в†ђ[90m at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)в†ђ[39m
Emitted 'error' event on Socket instance at:
в†ђ[90m at GetAddrInfoReqWrap.callback (dgram.js:289:12)в†ђ[39m
в†ђ[90m at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:17)в†ђ[39m {
errno: в†ђ[32m'ENOTFOUND'в†ђ[39m,
code: в†ђ[32m'ENOTFOUND'в†ђ[39m,
syscall: в†ђ[32m'getaddrinfo'в†ђ[39m,
hostname: в†ђ[32m'127.0.0.1/api/v1/somethig'в†ђ[39m
}
Answer
Solution:
In HTTP, the 'path' part of your URL gets sent to your server on the first line.
While theudp:
scheme seems valid (although I didn't find any official document detailing it), I don't think there's a mapping between the 'path' part and anything that the client would send you.
So broadly speaking, if you (or someone) uses theudp:
scheme, there's no agreed upon or standard that documents what to do with this, so I doubt you'll get that segment submitted to you in any way.
Each protocol/scheme has to explicitly define what to do with many segments of the URI. A raw UDP or TCP socket doesn't really send any sort of data until you explicitly tell it to. Similarly you also won't know server-side what host was used to connect to you, whereas with HTTP the expectation is that this goes in theHost
header.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: filter_sanitize_string deprecated
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.