获取'语法错误'在玉视图上

时间:2014-03-16 09:32:20

标签: javascript node.js express pug

我正在尝试一个简单的聊天应用程序,使用简单的jade视图引擎和express。当我运行我的应用程序时,我在下面的视图代码上遇到语法错误。但就这么简单。

    extends layout
    block scripts
      script(type='text/javascript', src='/socket.io/socket.io.js')
      script(type='text/javascript')
        var socket = io.connect('http://localhost:8080');
        socket.on('chat', function(data) {
          document.getElementById('chat').innerHTML = '<p><b>' + data.title + '</b>: ' +   data.contents + '</p>';
        });

        var submitChat = function(form) {
          socket.emit('chat', {text: form.chat.value});
          return false;
        };
    block content
     div#chat
      form(onsubmit='return submitChat(this);')
        input#chat(name='chat', type='text')
        input(type='submit', value='Send Chat')

我收到了这个错误:

SyntaxError: views/chat.jade:9 
7| document.getElementById('chat').innerHTML = 
8| '<p><b>' + data.title + '</b>: ' + data.contents + '</p>'; > 
9| }); 
10| var submitChat = function(form) { 
11| socket.emit('chat', {text: form.chat.value}); 
12| return false; 
Unexpected token ; at Function (<anonymous>) at assertExpression 

1 个答案:

答案 0 :(得分:3)

好像你错过了一个简单的,请参阅Jade Reference

例如:

script(type='text/javascript')
  var socket = io.connect('http://localhost:8080');

将给出

<script type="text/javascript">
  <var>socket = io.connect('http://localhost:8080');</var>
</script>

您收到Unexpected token错误。

script(type='text/javascript').
    var socket = io.connect('http://localhost:8080');

为了向您展示示例中的差异,上面的代码将给出:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
  var socket = io.connect('http://localhost:8080');
</script>

所以你的代码应该是这样的:

extends layout
block scripts
  script(type='text/javascript', src='/socket.io/socket.io.js')
  script(type='text/javascript').
    var socket = io.connect('http://localhost:8080');
    socket.on('chat', function(data) {
      document.getElementById('chat').innerHTML = '<p><b>' + data.title + '</b>: ' +   data.contents + '</p>';
    });

    var submitChat = function(form) {
      socket.emit('chat', {text: form.chat.value});
      return false;
    };
block content
 div#chat
  form(onsubmit='return submitChat(this);')
    input#chat(name='chat', type='text')
    input(type='submit', value='Send Chat')