EJS Include templates giving '`include` requires the 'filename' option.' Error even when filenames passed.

394 views Asked by At

I am passing the full filename as advised on different questions yet I'm still receiving this error:

`include` requires the 'filename' option.

Here is my app.coffee

db = require('./db')

http = require('http')
fs = require('fs')
ejs = require('ejs')
# bs = require('bootstrap')

db.albums.find({}, {}, 
    (err, data) ->
        if err?
            console.log("Error: failure to retrieve albums.")
            return
        else
            initiate_server(data)
            return
    ).sort({'play_counts': -1}).limit(20)

initiate_server = (albums) ->
    http.createServer(
        (req, res) ->
            res.writeHead(200, {'Content-Type': 'text/html'})
            # res.end(String(data))

            fs.readFile('views/index.ejs', 'utf-8',
                (err, data) ->
                    if err?
                        res.end('error occured')
                    else
                        dir_path = __dirname + '/views'

                        renderedHtml = ejs.render(data, 
                                {
                                    albums: albums,
                                    headerText: 'TempText',
                                    head: '/partials/head.ejs',
                                    header: dir_path + '/partials/header.ejs',
                                    footer: dir_path + '/partials/footer.ejs'
                                }
                            )
                        res.end(renderedHtml)

                        # res.end(data)
            )
            return
    ).listen(8080)
    console.log ('Server listening')
    return

and here is my index.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <% include(head) %>
</head>

<body class="container">
    <header>
    </header>

    <main>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th> Rank </th>
                    <th> Album </th>
                </tr>
            </thead>
            <tbody>
              <% albums.forEach(function(album, count){ %>
                <tr>
                    <td><%= count %></td>
                    <td><%= album.album_name %></td>
                </tr>
              <% }) %>
            </tbody>
        </table>
    </main>

    <footer>
    </footer>

</body>
</html>

I've tried just passing the relative path too and I'm still getting this error, any suggestions? Thank you.

0

There are 0 answers