简体   繁体   中英

Sails.js index.ejs controller logic

I'm a little confused on how to add logic to the home page of my Sails.js app. Right now, its just a static page, but I would like to include data on the homepage (index.ejs). I have a MainController and I included a index: function that is pulling in the data. I can't figure out how to configure my route to allow for this.

From What you wrote I am guessing you are using Express js and ejs template.

If you are using the Express render method in your mainController to render your index.ejs you can send the data with response like:

    res.render('index', { data: 'my data' }, function(err, html){
        // handle callback and error
    });

so here you have sent some data

then in your index.ejs file you can get data using ejs tags like:

<div><%= data %></div>

Tell me more about your mainController method if this is not useful

The static-folder is for static-content only (as the name is telling us). If you want your website to show content dynamically you need to create a controller like that:

module.exports = {
    index: function(req, res){
        res.view(); // sending the view in /views/{controller_name}/index.ejs
        return;
    }
}

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM