Friday 2 June 2017

How to pass function required from another .js file into an index.ejs view via app.js

graphs.js: contains a function that makes an API call and returns an object with an HTML link that will be used to embed a graph.

app.js: contains the following (graphs.js has been required):

var express = require("express");
var app = express();
var graphs = require("graphs"); <---

app.use(express.static(__dirname + '/'));
app.set("view engine", "ejs");

app.get("/", function(req, res){
    res.render("index");
    CAN I PASS THE FUNCTION THROUGH HERE?
});

//server
app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Dev server started!");
});

index.ejs: I am attempting to pass the graphs.graph API function call through app.js so it is usable and executed in my index.ejs

For example:

<div class="graph-test">
    <% graphs.graph() %>
</div>

Any advice, best practice comments, or any other methods of doing this would be greatly appreciated. Thank you!



via Lukon

No comments:

Post a Comment