如何在node.jsexpression式的基础上调用函数
我想在哪个button被点击的基础上调用函数。
我在这里有EJS文件叫做test.ejs
<form> <input type="submit" value="Click here 1"></input> <input type="submit" value="Click here 2"></input> <input type="submit" value="Click here 3"></input> <input type="submit" value="Click here 4"></input> </form>
我有JS文件名test.js
var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res) { console.log("Get the Request from test...!!!"); }); function button1(){ console.log("User Click Button 1"); } function button2(){ console.log("User Click Button 2"); } function button3(){ console.log("User Click Button 3"); } function button4(){ console.log("User Click Button 4"); } module.exports = router;
当button1用button2,3和4点击时,我想调用fuction button1()
。
什么是程序。 请帮助我一些例子。
您可以使用AJAX或WebSockets:
AJAX:
(我假设你有jQuery)
HTML:
<button id='button1'> Test </button>
JS(客户端):
$('#button1').click(function(){ console.log('button clicked'); $.ajax({url: 'test1', success:function(res){ console.log('server response is', res); }}); });
JS(服务器):
function buttonAction1(res){ res.send('ok'); } router.get("/test1", function (req, res) { buttonAction1(res); });
WebSockets简介:
我更喜欢使用像socket.io的东西。 这里有一些你可以使用的教程 。