bobby bobby - 10 months ago 64
Node.js Question

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:


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


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

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

console.log( ??


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



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

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.