Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 504
Sam Avatar asked Oct 30 '25 01:10

Sam


2 Answers

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.

like image 167
Dhananjayan Avatar answered Nov 02 '25 05:11

Dhananjayan


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') 
like image 23
Robert Moskal Avatar answered Nov 02 '25 07:11

Robert Moskal