On Saturday, September 3, 2016 at 12:05:41 AM UTC-4, Junior Domingos wrote:
>
>  Does anyone know in Node.js to call an external file function by clicking 
> a button in html?
>

Maybe. It depends on what you mean by 'external file function'.

If it's a commonjs module that is the external file function, then you can 
require it into an http server.

server.js:

var http = require('http');
var fs = require('fs');
var externalFunction = require('./externalfunction.js');
http.createServer(req, res) { 
   if (req.method == 'post') {
      externalFunction();
      res.end('ok');
   } else {
      res.setHeader('content-type: text/html');
      fs.createReadStream('button.html').pipe(res);
   }
}).listen(8080);

And some HTML:

button.html

<form action="." method=post>
   <button>Do the thing</button>
</form>


If you run that server script, it will listen on port 8080. 
http://localhost:8080/ should show the button, and if you click it, the 
server should call the function.

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/0c076ed4-66fd-4e5e-88d1-a5892144236d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to