简体   繁体   中英

get values from client side to server side (node.js + express.js)

I'm still trying to understand the concepts of node.js so please don't blame me if this is a dumb question..

In node.js, is it possible to get a value from index.jade to index.js ?

For example:

index.jade

a(href="/bla" name="someName") Blabla

index.js

router.get('/bla', function(req, res){

//get value of name ("someName") or string ("Blabla")

console.log(req.body.name) ??

});

If this is not possible, I would like to know why...

Thanks.

No, it's not possible, for the simple reason that the name attribute in your HTML doesn't get passed to the server ( any server, not necessarily a Node-based server).

If you want to pass a value in a GET request, you generally pass it as part of the URL:

a(href="/bla?name=someName") Blabla

This will generate the following HTML:

<a href="/bla?name=someName">Blabla</a>

In your server code, you can access the value using req.query.name .

Taking this a step further: if you have a variable available to your template called "name", you can use something similar, but a bit more dynamic:

a(href="/bla?name=" + encodeURIComponent(name)) Blabla

encodeURIComponent makes sure that any "special" characters (that may have a special meaning in URL's) will be encoded properly.

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