Reputation: 72
I was creating an EJS scriptlet tag that included the native browser JS function fetch
to make my HTTP request. I was then greeted with a reference error saying that fetch
was not defined.
Is there a way to use that function in EJS? Or does EJS only support Node?
Upvotes: 0
Views: 228
Reputation: 707876
It's hard to know precisely what you're trying to do with fetch()
in your EJS template without seeing your actual attempted code. Here are things you can do:
You can insert a <script>
tag into your EJS template and put Javascript into that tag that will execute in the browser. That script could contain a call to fetch()
in the browser.
You can use nodejs equivalent of fetch()
such as node-fetch and use that to retrieve content that you then insert into your rendered EJS template. This type of fetch()
call would be server-side code (running in nodejs) to retrieve data (not in your actual template). That data could then be sent as arguments to the call to render your EJS page so that data could be rendered into the web page by EJS.
You could do client-side rendering in the browser using EJS and you could use the browser's fetch()
to retrieve data before doing the client-side render.
Upvotes: 1