1

The basic problem is as follows:

Passing an object from my c# program, which is serialize and send through a socket. Achieved this in node.js/express application with socket.io.

Need to pass this to the client and using pug as the template engine.

Everything tried so far just doesn't seem to be working. Can't access the elements in the object. Pretty new to node and express so please bear with me.

app.js :

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./app_server/routes/index');
var users = require('./app_server/routes/users');

var app = express();
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

// view engine setup
app.set('views', path.join(__dirname, 'app_server', 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

layout.jade :

doctype html
html
  head
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
    title= title
    link(rel='stylesheet', href='/bootstrap/css/amelia.bootstrap.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(src="https://cdn.socket.io/socket.io-1.4.5.js")
    script.
      var socket = io.connect('http://localhost');
      socket.on('news', function (data) {
      console.log(data);
      socket.emit('my other event', { my: 'data' });
      });

  body
    block content

    script(src='/javascripts/jquery-1.11.1.min.js')
    script(src='/bootstrap/js/bootstrap.min.js')

index.jade :

extends layout

block content
  h1= title
  p Welcome to #{title}

  //THIS IS WHERE I DON'T KNOW HOW TO ACCESS THE OBJECT AND ITS PROPERTIES

Can see the object in the console so the data is appearing.

Tried many different variations, but nothing seems to be working. Any input or guidance would be welcome.

Thanks in advance.

NAVIN
  • 3,193
  • 4
  • 19
  • 32
MojoJojo86
  • 11
  • 6
  • where is the code on the client? – LifeQuery Nov 20 '16 at 17:59
  • Well isnt the page rendered in the client browser. I presumed all this would be done on the server and then the page just sent out? – MojoJojo86 Nov 20 '16 at 19:02
  • yes it is rendered in the browser, but are you also initiating the socket in the browser? that's what we don't see. If you don't know what I mean, have a look at this example http://stackoverflow.com/a/9916153/3017785 – LifeQuery Nov 20 '16 at 21:50
  • I understand what you mean now. Im not emitting anything on the client. The problem i think i had when i put the code in the pug file gave me an error about running that code at top level in the template. If i cant do that i figured i would have to pass the object in through a route or controller. That example you provided me though did kind of make me think my code is just full on unnessessary code. – MojoJojo86 Nov 20 '16 at 22:12
  • Still can't get it to work. I've used the example you provided and the socket.io documents. Im able to get the object to appear in console just not render in the page. – MojoJojo86 Nov 21 '16 at 12:48
  • update your question with the client side code you tried – LifeQuery Nov 21 '16 at 13:20
  • Done. Thanks for help in advance. – MojoJojo86 Nov 21 '16 at 13:51

0 Answers0