简体   繁体   English

Node.js将JavaScript连接到app.js

[英]Node.js connect javascript to app.js

This is a super simple question but i am really new to Node, i'm reading all the beginners guide now so no need to direct me to them! 这是一个非常简单的问题,但是我真的是Node的新手,我现在正在阅读所有初学者指南,因此无需指导我! guides tell you how to use node not how to get the thing working! 指南告诉您如何使用节点而不是如何使事物正常工作!

I'm finding node.js tutorials assume way too much knowledge and set up. 我发现node.js教程假定了太多的知识并进行了设置。

i have my app.js file which contains: 我有我的app.js文件,其中包含:

SERVER SIDE 服务器端

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

// New Code

var mongo = require('mongojs');
var db = mongo('site');
var app = express();

// all environments
app.set('port', process.env.PORT || 80);
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}



app.get('/libraries/*', function(req, res){
    req.url.replace('/libraries', ''); // remove libraries prefix
    var path = req.url.split("/libraries").pop();
    console.log(path);
    switch (path) {
        case '/socket.io/socket.io.js':
            res.sendfile('node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js');
            break;
    }

});

// setup server
var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

// setup socket
var io = require('socket.io').listen(server);

CLIENT SIDE: 客户端:

var socket = io.connect();
socket.on('ready', function (data) {
    marketData = data; // raw data from the DB 
});

All good and well. 一切都很好。

I want to console log 'test' into the website console. 我想将“测试”控制台登录到网站控制台。

My assumption is i define a function server side and call it client side... How on earth do I do this?!?! 我的假设是我定义一个功能服务器端,并将其称为客户端端...到底该怎么做?!?!

I just want them to talk to each other. 我只希望他们彼此交谈。

Server side i would like to define a var and call it client side. 服务器端我想定义一个var并将其称为客户端。

My assumption: 我的假设:

Server side: 服务器端:

function test () {
console.log ('test');
}

Client side: 客户端:

test();

Console on my website says: 'test'. 我网站上的控制台上说:“测试”。

Expeting that you want to do communications with Socket.io, add to the bottom of your server script: 解释要与Socket.io通信,将其添加到服务器脚本的底部:

io.sockets.on('connection', function (socket) {
    // Do what you need to do here and finally emit "read"
    // here setTimeout simulates your database query or similar
    // process of building the data you want to send to the client
    setTimeout(function (data) {
        socket.emit('ready', data);
    }, 1000);
});

then at the client side have: 然后在客户端有:

var socket = io.connect();
socket.on('ready', function (data) {
    marketData = data; // raw data from the DB 
    test();
});

When your client connects, the server will run your business logics and send back the data by emitting "ready" event to the client. 当您的客户端连接时,服务器将运行您的业务逻辑并通过向客户端发出“就绪”事件来发送回数据。 I added call for test() in your client event handler that might help on understanding the process. 我在您的客户端事件处理程序中添加了对test()的调用,这可能有助于理解该过程。

To get a connection between your javascript (client side) and the server the below should work: 要在您的javascript(客户端)和服务器之间建立连接,以下方法应该可以工作:

Client side: 客户端:

socket.on('news', function (data) {
  console.log(data);
});

Server side: 服务器端:

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
});

Will console log your data which is an object { hello: 'world' } 将控制台记录您作为对象的数据{hello:'world'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM