How to get the base or root url in sailsjs?

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.

Upvotes: 3

Views: 3667

Answers (3)

Eli Peters
Eli Peters

Reputation: 420

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().

Upvotes: 0

Urgo
Urgo

Reputation: 710

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

Upvotes: 3

bredikhin
bredikhin

Reputation: 9025

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 + '/';

Upvotes: 8

Related Questions