Alvaro Molina Alvaro Molina - 1 year ago 72
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


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 "/"

Answer Source

The request.url is the url that the user is requesting. If they request, then the url is '/'. If they access, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download