I have the following middleware for static files defined in express:
app.use(express.static(__dirname + '/public'));
app.use('/lessons', express.static(__dirname + '/lessons', { redirect : false }));
Inside lessons folder I have a couple of lesson folders
lessons
|-- lesson01
|-- lesson02
.
.
.
Inside every lesson folder there's a lesson.md
file that I render. This file has references to images in the same lesson folder. I want to be able to just use for example img01.png
inside the markdown file to reference the image, but express expects the image to be found in /lessons/img01.png
and not in /lessons/lesson01/img01.png
.
Using ./img01.png
as a reference to the image in the markdown file didn't help either.
Any idea on how yo tell express to look in the same directory for the static file ?
Thanks
Edit :
A little more info,
The route to get the lesson file is :
app.get('/lessons/:name', function(req, res, next) {
res.locals.controller = 'lessons';
var name = req.params.name;
var lesson = lessons[name];
if (!lesson) {
return next();
}
lesson.path = 'lessons/' + name;
lesson.file = lesson.path + '/' + name + '.md';
markdown(lesson.file, function(result, err) {
if (err) {
console.log(err);
}
lesson.body = result;
res.locals.lesson = lesson;
res.render('lesson');
});
});
Also, I am using middleware called connect-slashes to remove the slash from the end of each URL. (As result of this SO question)
var slashes = require('connect-slashes');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use('/lessons', express.static(__dirname + '/lessons', { redirect : false }));
// add middleware to remove trailing slash in urls
app.use(slashes(false));