In the following code I'm creating an API and I want to pass a pathname and check it using switch statements to pass GET, POST, etc.
But it seems I am doing something wrong because it doesn't seem to pass the pathname but the localhost:8080..
var http = require('http');
var URL = require('URL');
var server = http.createServer(function (req, res) {
var parsedURL = URL.parse(req.URL, true);
switch (parsedURL) {
case '/api/something':
if (parsedURL.query.id) {
findProductById(id, req, res);
}
else {
findAll(req, res);
}
break;
default;
res.end('End of connection');
}
});
server.listen(8080);
console.log("Running");
What am I doing wrong? Am I using the URL properly? Or missing something with the parsing?
Thanks for help!
switch(parsedURL.pathname) {if you want the pathname?