Reputation: 379
I am trying to serve .png using the express framework, but keep receiving a "Cannot /get" error.
var express = require('express'),
mongoose = require('mongoose'),
fs = require('fs'),
Schema = mongoose.Schema;
app = express(),
port = process.env.PORT || 3000;
app.use('/public/media', express.static(__dirname + '/public/media/img0.png'));
app.listen(port);
console.log(__dirname + '/public/media/img0.png');
I print out the directory name of my file to ensure I have the right path. Delving deeper into the develepor tools however shows the resource could not be loaded due to 404 error.
My file structure is something like this.
-- admin
-- app.js public
-- media
-- img0.png img1.png
From what I have seen, my static file server should work, so I am at a bit of a loss. Thoughts?
Upvotes: 1
Views: 58
Reputation: 70055
Note this line:
app.use('/public/media', express.static(__dirname + '/public/media/img0.png'));
That says that if the browser asks for /public/media
, then please serve up the PNG file. So that will work.
If the browser asks for /public/media/img0.png
, then that will be a 404.
It seems likely that you meant this instead:
app.use('/public/media', express.static(__dirname + '/public/media'));
That will serve a 404 for /public/media
but will serve any image you specify in that directory if the corresponding image file exists. So if the browser asks for /public/media/img0.png
, it will get it.
Upvotes: 2
Reputation: 211540
You might try applying static
to the whole directory:
app.use('/public/media', express.static(__dirname + '/public/media/'));
Upvotes: 0