Alvaro Molina Alvaro Molina - 2 months ago 9
AngularJS Question

NodeJS request.url is always "/"

I just started to learn NodeJS and I want to know the URL of a request to execute one function or another but when I use

request.url

always return "/". Im using NodeJS without another modules like Request and AngularJS in the front-end and running the web in the file:///C:/...

var http = require("http");
http.createServer(function(peticion, respuesta){
console.log(peticion.url) //always return "/"
}.listen(8888)

Answer

The request.url is the url that the user is requesting. If they request yoursite.com, then the url is '/'. If they access yoursite.com/nodejs.txt, the request url is '/nodejs.txt'.

If you are looking to have different behavior per URL, you might want to say

if ("/resource" === request.url) 
   //do stuff
} else if ("/place" === request.url) {
    ........

Keep in mind, you are programming on the bare metal of node. If you are looking to actually develop an app, using a framework like express would make this easy.