将对象传递到表达

时间:2016-11-20 15:58:02

标签: javascript node.js express socket.io

基本问题如下:

从我的c#程序传递一个对象,该程序是序列化并通过套接字发送。使用 socket.io node.js / express 应用程序中实现此目的。

需要将此传递给客户端并使用pug作为模板引擎。

到目前为止所做的一切似乎都没有起作用。无法访问对象中的元素。对节点和表达很新,所以请耐心等待。

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

可以在控制台中看到对象,以便显示数据。

尝试了许多不同的变体,但似乎没有任何效果。欢迎任何意见或指导。

提前致谢。

0 个答案:

没有答案