Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access assets/images from the view in Sails.js?

Tags:

html

ejs

sails.js

I simply want to add an image in a view in my Sails-Project

My view-file has the location

views/album/albums.ejs

and the image is located in

assets/images/placeholder.png

if I add the image like this

<img src="../../assets/images/placeholder.png">

I get this error

GET    http://localhost:1337/assets/images/placeholder.png    404 (Not Found)

Am I missing something?

like image 580
klanm Avatar asked Sep 13 '13 12:09

klanm


3 Answers

Sails use Grunt (Gruntfile.js in root of project) to execute some tasks during sails lift. One of that tasks is to copy files from assets directory to .tmp/public/ directory (in development version). So if u add your file to assets directory you will need to restart sails (sails lift) to get it accessible from .tmp/public/ (what is public accessible directory root). Also its important to note that if u put files directly to .tmp/public/ it will be accessible instant, but on next sails lift it will be deleted, since one of Grunt tasks is to clear that directory before copy new files. All of this u can find on sails documentation (assets and asset-management) and by reading Gruntfile.js in root of your project

like image 177
zmilan Avatar answered Oct 05 '22 20:10

zmilan


<img src="/images/placeholder.png">

should work. the assets folder is the equivalent to adding a folder with the static middleware in express.

asset documentation

like image 28
snyx Avatar answered Oct 05 '22 21:10

snyx


Looks like you have grunt hook removed.

When removing the grunt hook you must also specify the following in .sailsrc in order for your assets to be served, otherwise all assets will return a 404.

{
  "paths": {
    "public": "assets"
  }
}
like image 36
mrded Avatar answered Oct 05 '22 22:10

mrded