Sam
Sam

Reputation: 1229

Images not showing pug, express node.js

I've gone through what I should do for setting up images to show in my application:

http://www.tutorialsteacher.com/nodejs/serving-static-files-in-nodejs

I have the current directory structure:

rootproject
-public
--images
---image.jpg
server.js

server.js:

// require all dependencies
var express = require('express');
var app = express();
var PythonShell = require('python-shell');
var path = require('path');

// set up the template engine
app.set('views', './views');
app.set('view engine', 'pug');

app.use("/public", express.static(path.join(__dirname, 'public')));

index.pug:

html
    head
        title= title
    body
        img(src='/static/images/image.jpg' , alt='some image') 
        h1= message

This does not work though and shows me in the console: GET http://localhost:3000/static/images/image.jpg 404 (Not Found)

Upvotes: 4

Views: 10694

Answers (3)

Dhananjayan
Dhananjayan

Reputation: 582

In my case, In my server.js file it has

app.use(express.static(path.join(__dirname, 'public')));

instead of like in the above question

app.use("/public", express.static(path.join(__dirname, 'public')));

so, that image path starts with /public not working for me. instead, I used images/image.jpg. It worked for me.

Upvotes: 4

YouneL
YouneL

Reputation: 8369

The path /static/images/image.jpg does not exist in the public directory, you have to remove /static and it should work:

img(src='/images/image.jpg' , alt='some image') 

Upvotes: 2

Robert Moskal
Robert Moskal

Reputation: 22553

The way your static routes are set up, you should refer to your images in /public, not /static.

img(src='/public/images/image.jpg' , alt='some image') 

Upvotes: 2

Related Questions