Is there a sails way of extracting the root or base url of an app? I basically need this for sending activation emails with links.
-
1Wish I could answer your question, but thanks for the inadvertent pointer to sails! Looks really slick. That said, have you tried asking in their IRC channel? irc://irc.freenode.net/sailsjs – brandonscript Dec 10 '13 at 07:48
-
I got my answer here but thanks for the heads up about irc. – Carlos Feliciano Barba Dec 10 '13 at 18:04
3 Answers
Sails.js is based on Express, so from within your action you can do the following:
var protocol = req.connection.encrypted?'https':'http';
var baseUrl = protocol + '://' + req.headers.host + '/';

- 8,875
- 3
- 40
- 44
-
You're missing a + before req.headers.host. It works perfectly thanks. – Carlos Feliciano Barba Dec 10 '13 at 18:00
-
@bredikhin I send an email to confirm account when registering users, so I want to check if the link is correctly written. How ca I know where the request of the link is coming from? – alexventuraio Dec 01 '15 at 18:18
Just to give an updated answer for anyone that finds this page...
In Sails v0.10 you can access it through req.baseUrl
or sails.getBaseurl()
in your views.
Answer taken from: How to get current domain address in sails.js
A couple other options available... sails.config.appPath
or require('path').resolve('.')
and both will return a path from your app's drive to the app root. Caveat is local development as C:\websites\mywebsite\remote
is not very useful and any loaded asset will be blocked by the browser.
This source uses the require.resolve() function to calculate the absolute path of appPath
and appending assets/images
.
req.file('avatar').upload({
dirname: require('path').resolve(sails.config.appPath, 'assets/images')
},function (err, uploadedFiles) {
if (err) return res.negotiate(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!'
});
});
In the above example you could change 'assets/images'
to '../assets/images'
to store file uploads below the root of your website - if you are into hiding your stuff!
Here is a good article about require.resolve()
.

- 406
- 4
- 10