node ninja
node ninja

Reputation: 33056

How to call a node.js function from an express rendered ejs webpage

I have an express server that has a button that issues a POST which activates a function in my node.js server.

Instead of sending a post, I would like to do something kind of like AJAX because I don't want a new page to load. If I use a Javascript button, is there a way I can directly call a function defined in the node.js server instead of having to send a post?

I noticed that JSON values can be passed into the .ejs webpage when it's rendered, but I haven't been able to get it to work for functions.

Upvotes: 0

Views: 1174

Answers (2)

jimr
jimr

Reputation: 11230

Take a look at nowjs. It lets you very easily invoke functions on the server from the client and vice-versa:

From the site:

  • NowJS creates a magic namespace "now", accessible by server and client
  • Functions and variables added to now are automatically synced, in real-time
  • Call client functions from the server and server functions from client

Upvotes: 1

levi
levi

Reputation: 25161

You can use Socket.io to emit an event from the browser, and than listen for that event on the server to act on it.

Socket.io works flawlessly with express http://socket.io/#how-to-use

Upvotes: 1

Related Questions