Let's say my sample url is
and I say I have the following route
app.get('/one/two', function (req, res) {
var url = req.url;
}
The value of url
will be /one/two
.
How do I get the full url in Express? For example, in the case above, I would like to receive http://example.com/one/two
.
转载于:https://stackoverflow.com/questions/10183291/how-to-get-the-full-url-in-express
You need to construct it using req.headers.host + req.url
. Of course if you are hosting in a different port and such you get the idea ;-)
I found it a bit of a PITA to get the requested url. I can't believe there's not an easier way in express. Should just be req.requested_url
But here's how I set it:
var port = req.app.settings.port || cfg.port;
res.locals.requested_url = req.protocol + '://' + req.host + ( port == 80 || port == 443 ? '' : ':'+port ) + req.path;
Instead of concatenating the things together on your own, you could instead use the node.js API for URLs and pass URL.format()
the informations from express.
Example:
var url = require('url');
function fullUrl(req) {
return url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl
});
}
My code looks like this,
params['host_url'] = req.protocol + '://' + req.headers.host + req.url;
I would suggest using originalUrl instead of URL:
var url = req.protocol + '://' + req.get('host') + req.originalUrl;
See the description of originalUrl here: http://expressjs.com/api.html#req.originalUrl
In our system, we do something like this, so originalUrl is important to us:
foo = express();
express().use('/foo', foo);
foo.use(require('/foo/blah_controller'));
blah_controller looks like this:
controller = express();
module.exports = controller;
controller.get('/bar/:barparam', function(req, res) { /* handler code */ });
So our URLs have the format:
www.example.com/foo/bar/:barparam
Hence, we need req.originalUrl in the bar controller get handler.
I use the node package 'url' (npm install url)
What it does is when you call
url.parse(req.url, true, true)
it will give you the possibility to retrieve all or parts of the url. More info here: https://github.com/defunctzombie/node-url
I used it in the following way to get whatever comes after the / in http://www.example.com/ to use as a variable and pull up a particular profile (kind of like facebook: http://www.facebook.com/username)
var url = require('url');
var urlParts = url.parse(req.url, true, true);
var pathname = urlParts.pathname;
var username = pathname.slice(1);
Though for this to work, you have to create your route this way in your server.js file:
self.routes['/:username'] = require('./routes/users');
And set your route file this way:
router.get('/:username', function(req, res) {
//here comes the url parsing code
}
I actually discovered that by using this code below you can get your url. Then proceed to slicing it up and deciding what next.
app.use(function(req, res, next) {
console.log(req.originalUrl);
res.send(req.originalUrl);
});
Using url.format:
var url = require('url');
This support all protocols and include port number. If you don't have a query string in your originalUrl you can use this cleaner solution:
var requrl = url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl,
});
If you have a query string:
var urlobj = url.parse(req.originalUrl);
urlobj.protocol = req.protocol;
urlobj.host = req.get('host');
var requrl = url.format(urlobj);
Use this,
var url = req.headers.host + '/' + req.url;
make req.host/req.hostname effective must have two condition when Express behind proxies:
app.set('trust proxy', 'loopback');
in app.jsX-Forwarded-Host
header must specified by you own in webserver. eg. apache, nginxnginx:
server {
listen myhost:80;
server_name myhost;
location / {
root /path/to/myapp/public;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myapp:8080;
}
}
apache:
<VirtualHost myhost:80>
ServerName myhost
DocumentRoot /path/to/myapp/public
ProxyPass / http://myapp:8080/
ProxyPassReverse / http://myapp:8080/
</VirtualHost>
var full_address = req.protocol + "://" + req.headers.host + req.originalUrl;
or
var full_address = req.protocol + "://" + req.headers.host + req.baseUrl;`