Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to just serving directory #1

Open
nickcarenza opened this issue Feb 5, 2014 · 6 comments
Open

Fallback to just serving directory #1

nickcarenza opened this issue Feb 5, 2014 · 6 comments

Comments

@nickcarenza
Copy link

Can docserver fallback to using express.directory() on the current directory if it doesn't find an index.md?

@nickcarenza
Copy link
Author

Currently getting around this by adding return next(); at line 99 of docserver.js where a 404 would otherwise be returned. There is probably a better way to handle this though.

@natesilva
Copy link
Owner

I don’t want to add a dependency for Express, but there may be a way to do this, maybe as a callback. What about something like: a directoryHandler option that takes a callback. Call the directoryHandler for any directory that is found, if there isn’t an index document. (Regular 404 would still be returned if a non-existent directory is requested.)

@crucialfelix
Copy link

Maybe another package could use docserver and then add more features to it like directory listing. Personally though I think we all want it and adding more features isn't a bad thing.

Right now I had to create a serve.js and customize it the way I want. But ideally we could just use docserver in any directory and it would autoindex. I need this all the time !

Small packages are good though.

This was the first thing I did:

    var
        express = require('express'),
        docserver = require('docserver'),
        dirname = __dirname,
        app = express();

    app.use(express.directory(dirname));
    app.use(docserver({
      dir: dirname ,  // serve Markdown files in the docs directory...
      url: '/',
      watch: true,
      cache: false
    }));

    app.listen(3000);

Works great !

@nickcarenza
Copy link
Author

@crucialfelix Will that serve a directory listing though even if index.md is defined, overwriting docserver's functionality? My script ended up looking jsut like yours except with reversed app.use statements such that docserver has priority. That is why I had to add that hacky change to how docserver responds to 404 on index.md in a directory.

@crucialfelix
Copy link

at first I had use directory afterwards, but it failed to generate a directory at all. docserver would always show 404

I'm not sure if index.md would get override the way I have it

a fuller solution would be better. and I am not that fond of the directory display style. so I think another package that makes use of docserver would be useful. or if docserver can easily delegate the directory rendering

@bitdivine
Copy link

Hello,

I agree completely with not adding any dependencies. This module is good at converting markdown files to HTML, so in my opinion (who's asking? ;-) ) it should focus on that and do it well. There are other modules that generate a directory listing, e.g. serve-index: https://github.com/expressjs/serve-index or that define a directory index file, e.g. by mapping /data/ to /data/index.md so that it is then processed as above.

To make this work, however, docserver needs to behave like standard middleware, i.e. if it is asked for something other than a markdown file it should pass on the request. You mentioned next() above :-)

I need to serve a directory containing a mix of markdown and other files, so what I want to do is quite similar to @crucialfelix :

var express    = require('express');
var serve_directory_index = require('serve-index');
var serve_markdown = require('docserver');
var app = express();
app.use('/', serve_directory_index(__dirname, {'icons': true})); // directory listings
app.use('/', serve_markdown(__dirname)); // markdown
app.use('/', express.static(__dirname));  // images etc.
app.listen()

You could keep the existing behaviour with index.md with a flag, perhaps. Here is how one might render README.md by default, and fall back to a directory listing if there is no README:

var express    = require('express');
var serve_directory_index = require('serve-index');
var serve_markdown = require('docserver');
var app = express();
app.use('/', serve_markdown(__dirname, {index:'README.md'}));
app.use('/', serve_directory_index(__dirname, {'icons': true})); // directory listings
app.use('/', express.static(__dirname));  // images etc.
app.listen()

What do you think? Does it keep the soul of docserver intact whilst making it useful in more situations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants