Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access image from directory located outside the workspace? #490

Open
sachinavinaw opened this issue Jul 20, 2017 · 1 comment
Open

Comments

@sachinavinaw
Copy link

sachinavinaw commented Jul 20, 2017

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

@sachinavinaw sachinavinaw changed the title How to access image form directory located outside the workspace? How to access image fromm directory located outside the workspace? Jul 20, 2017
@sachinavinaw sachinavinaw changed the title How to access image fromm directory located outside the workspace? How to access image from directory located outside the workspace? Jul 20, 2017
@sixlettervariables
Copy link
Contributor

sixlettervariables commented Jul 20, 2017

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')));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants