1

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));
Community
  • 1
  • 1
Michael
  • 22,196
  • 33
  • 132
  • 187
  • How do you access the rendered `md` files? I assume you render them to HTML, do you have a separate route for that? – robertklep Mar 31 '13 at 20:24
  • @robertklep I render the `md` files on the fly through the `/lessons/:name` route and use `res.render(...)` to render the result. – Michael Apr 04 '13 at 16:44
  • 1
    Is it an option to access the lessons using `/lessons/lesson01/` (with trailing slash)? That would solve your problem instantly I think (and unless you use `strict routing` with Express, you don't have to change your route handler). – robertklep Apr 04 '13 at 18:26
  • @robertklep can you explain ? I didn't mention that was using a middleware to cut the slashes at the end of the url. I'll add it to the question. – Michael Apr 04 '13 at 20:13
  • 1
    The problem is the base directory from which images are location: the base dir of `/lesson/lesson01` (without slash) is `/lesson/`. If there *was* a trailing slash, the base dir for the image would be `/lesson/lesson01/`, and that's right where the image file is located (so you can use the static middleware). But that's not going to work if you're stripping slashes, obviously :) – robertklep Apr 04 '13 at 20:16
  • @robertklep That indeed solved the problem. I was hoping to use canonical urls without the slashes at the end. Can you think of any workaround ? – Michael Apr 04 '13 at 20:26
  • Two workarounds I can think of: fix the image paths in your md files (either after rendering to HTML, or on disk), or at server startup use `fs.readdir()` to find all `lessonXX` directories and install a static middleware for each separately. – robertklep Apr 05 '13 at 05:33
  • Another workaround: create a handler to catch requests for the images and handle reading the image file and sending the response yourself. – robertklep Apr 05 '13 at 07:54

0 Answers0