问题描述
我有一个带有单个按钮的简单HTML。 JavaScript(作为客户端脚本)捕获每次点击的时间。我对如何传递click
的事件/时间发生并使用Node.js进行显示以稍后将其传输到SNowflake数据库中感到困惑。
从我的评论看来,解决方法是JSDOM
,但是目前它引发了我一个错误。
https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming
https://stackoverflow.com/questions/38748996/using-document-object-in-nodejs
[更新-2020年11月]
在阅读下面的内容之后,我想到了将项目分为3个文件的新代码。在将数据插入到SNowflake中仍需要帮助,不确定如何将click事件绑定到sql文本。总体目标是将点击事件的日期时间插入到SNowflake的表中:
https://gist.github.com/aerrity/fd393e5511106420fba0c9602cc05d35
进行中代码:
server.js
console.log('Server-side code running');
const express = require('express');
const app = express();
// serve files from the public directory
app.use(express.static('public'));
//connect to sNowflake and load data
var sNowflake = require('sNowflake-sdk');
var connection = sNowflake.createConnection( {
account: 'xxxx',username: 'xxxx',password: 'xxxx'
}
);
//confirm connection is working
connection.connect(
function(err,conn) {
if (err) {
console.error('Unable to connect: ' + err.message);
}
else {
console.log('Successfully connected to SNowflake.');
// Optional: store the connection ID.
connection_ID = conn.getId();
}
}
);
// start the express web server listening on 8080
app.listen(8080,() => {
console.log('listening on 8080');
});
// serve the homepage
app.get('/',(req,res) => {
res.sendFile(__dirname + '/countClicks.html');
});
// insert data into sNowflake isues,how to bind click event?
connection.execute({
sqlText: 'INSERT INTO DEMO_DB.PUBLIC.INPUT_NODEJS(CLICK_DATETIME)',binds: [/clicked]
});
countClicks.js
const button = document.getElementById('clicks');
button.addEventListener('click',function(e) {
console.log('button was clicked');
fetch('/clicked',{method: 'POST'})
.then(function(response) {
if(response.ok) {
console.log('Click was recorded');
return;
}
throw new Error('Request Failed.');
})
.catch(function(error) {
console.log(error);
});
});
countClicks.html
<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<title>Node + Express + SNowflake example</title>
</head>
<body>
<h1>Node + Express + SNowflake example</h1>
<p id="counter">Loading button click data.</p>
<button id="myButton">Click me!</button>
</body>
<script src="countClicks.js"></script>
</html>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)