如何在节点中传输数据

时间:2018-11-30 23:17:42

标签: node.js function nodes ejs

该网站为index.ejs,包含两个下拉菜单和一个用于发送数据的按钮。节点从app.js开始,main.js包含javascript内容。目标是按照stopwatch.js中的配置将数据写入console.log中。在当前状态下,console.log通知按钮事件,但不传输下拉菜单的任何数据。

能帮您找到正确的语法,以将菜单数据发送到stopwatch.js的console.log吗?

index.ejs

<select id = "zeit">
<option value="Intervall1">Zeitintervall ...</option>
<option value="Intervall2">DSB Halle</option>
<option value="Intervall3">DSB Feld</option>
</select>
<select id = "countd">
<option value="Countd1">Zeit bis Start ...</option>
<option value="Countd2">15 min</option>
<option value="Countd3">30 min</option>
</select>
<button id="intervalle" class="thoughtbot" style="margin-top:15px;">Update</button>

app.js

var express     = require('express')
app             = module.exports = express()
server          = require('http').createServer(app)
io              = require('socket.io')(server, {
transport: "websocket",
pingInterval: 10000,
pingTimeout: 5000,
})
Stopwatch       = require('./models/stopwatch.js')
logger          = require('morgan');

app.use(logger('dev'));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index', {
title: 'Archery Traffic Light'
});
});

app.use(express.static(__dirname + '/node_modules'));
app.use(express.static(__dirname + '/public'));

var stopwatch = new Stopwatch();
io.sockets.on('connection', function (socket) {
console.log('[socket.io] Client connected ...\n' );
socket.on('click:intervalle',   function () { stopwatch.intervalle(); });
});

var port = process.env.PORT || 5000;
server.listen(port, function(){console.log("listening port " + port);
});

main.js

var socket = io.connect();
$('#intervalle').click(function() { socket.emit('click:intervalle'); });

stopwatch.js中的相关内容

Stopwatch.prototype.intervalle = function() {
    console.log('setzeitintervall');
};

0 个答案:

没有答案