krakenjs/kraken-js

How to access image from directory located outside the workspace?

sachinavinaw opened this issue · 1 comments

I am new to express Js.
My application structure is as following

myApp
|__public
|__routes
|__views
|__app.js
|__package.json

I want to load an image from another directory located outside the workspace.

In app.js, I have used:

var staticResource='C:\Users\Sachin\Desktop\profile\img';
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(staticResource, 'public2')));

And in pug:

img(src='/public2/one.jpg')

When I run my application, it displays

Failed to load resource: the server responded with a status of 404 (Not Found)

It is looking in

localhost:3000/public2/one.jpg for image.

Can anyone help me?

Thanks in advance

This is more of an expressjs issue than krakenjs (which builds on top of it), but an important thing to note with express.static is this:

Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.

So, if you wanted that behavior you should call app.use() passing in the route prefix as the first argument:

// omit the first arg to app.use if you do not want the /public prefix for these assets
app.use('/public', express.static(path.join(__dirname, 'public')));

var staticResource='C:/Users/Sachin/Desktop/profile/img';
app.use('/public2', express.static(path.join(staticResource, 'public2')));