F
F
flashboom2019-06-22 19:02:12
Node.js
flashboom, 2019-06-22 19:02:12

EJS (Node.js ) How to pass query string parameters to .ejs file?

Good day. I'm using the EJS template engine. On the main page (index.ejs) there is a link like this:
5d0e4f0a8353d118599941.png
In app.js there is a code that should display the request parameters, but nothing is output to the console.
5d0e4dc88507b465313219.png
But if you change the path from href="/ create ?id=3&name=Tome" to href="/ about ?id=3&name=Tome" (although the about page doesn't exist)
and change it to app.js,
5d0e4e38a268e597010758.png
then everything works.
The console displays 5d0e4d85b7a9b996642016.png
How to pass query string parameters to the .ejs file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
ned4ded, 2019-06-22
@flashboom

Good day.
I'm not an expert in either node or express, but I'll try to chime in a bit about the lack of output of parameters to the console. Given that you are using the middleware without calling next(), and without sending a response back, then you must have completed processing requests on one of the first mw - this is most likely the reason that /create does not display something - or to the console. Without a normal link to the source, it's hard to say something.
BUT! because your question is in no way related to the situation you described, then you can pass query parameters to the template file as follows:

app.get('/create', (req, res) => {
  res.render('name_of_ejs_file', req.query)
})

However, you must have the engine configured correctly and the mw chain must reach the handler hanging on the /create path, otherwise there will be no result.
Which, in fact, is basic information on express, in connection with which I suggest that you first read the guides on the official website , or immediately read some book / take some courses on nodejs + express. It seems to me that you have certain problems with understanding the basic concepts of express, without knowing which bugs will occur in the simplest things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question