1

This seems to me like a pretty basic question, but I haven't been able to find an answer.

I'm using express with ejs as template engine and the following dir structure:

 |-static
 |---css
 |---img
 |---js
 |-views

I have static routing defined for static folder:

app.configure(function(){
        app.set('views', __dirname + '/views');
        app.set('view engine', 'ejs');
        app.use(express.bodyParser());
        app.use(partials());
        app.use(express.methodOverride());
        app.use(express.static(__dirname + '/static'));
        app.use(app.router);
        app.enable("jsonp callback");
    });

In Views folder i keep all of my ejs files - one layout.ejs and the rest are files with the actual content of the specific page.

I've defined the following routes:

app.get('/', function(req,res){
    locals.date = new Date().toLocaleDateString();
    res.render('home.ejs', locals);
});

app.get('/about', function(req,res){
    locals.date = new Date().toLocaleDateString();
    res.render('about.ejs', locals);
});
app.get('/contact', function(req,res){
    locals.date = new Date().toLocaleDateString();
    res.render('contact.ejs', locals);
});

which takes layout.ejs and renders it with the page together.

Obviously I don't want to add a new route each time I add a new page, I want it to be done automatically.

So I am guessing it should have to do with defining another app.use(express.static(__dirname + '/views')); ? also I don't want the url to show /about.ejs but to show /about

Can someone please point me in the right direction ?

Thanks !

1 Answer 1

3

You can write your own routing logic, for example

function customRouter (req, res, next)
{
    var locals = {};
    var controllerName = req.params.controllerName;
    res.render(controllerName + '.ejs', locals);
}

app.get('/:controllerName', customRouter);

This is a simple example however it should give you the trick. You can modify it according to your own needs.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a followup question about the method you sugegsted. Trying to access file located in sample/file doesn't work, but accessing file.ejs works (both files are located in /views/sample and /views respectively). Do you know why ?
yes. here's my app.js : pastebin.com/a81yBPHC and my server.js: pastebin.com/vdsN8rcA . I have edited it like you said, but now that i have added a directory under views : views/lessons and i have lesson01.ejs there and trying to reach it, i get 404 error.
You should read the req.url and check that there is folder with the same name of your first uri component. You can check that with fs.existsSync(filePath). If the directory is exist, then you should check for the new directory by adding second uri to your control path. By the way, in this method, you should use app.get('/*', customRouter); since you are using your own custom routing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.