【问题标题】:NodeJS SyntaxError: expected expression, got '<'NodeJS SyntaxError:预期的表达式,得到'<'
【发布时间】:2016-03-08 16:30:50
【问题描述】:

我正在使用 NodeJS 和 Socket.io。这是我的 index.js

    var app = require('http').createServer(handler);
    var io = require('socket.io').listen(app);
    var fs = require('fs');

    app.listen(3000);
    console.log("Listening on port 3000");

    function handler(req, res) {
      fs.readFile(__dirname + '/index.html', function( err, data ) {
        if( err ) {
          res.writeHead( 500 );
          return res.end('Error loading index.html');
        }

        res.writeHead( 200 );
        res.end( data );
      });
    }

这是我的 index.html

<!DOCTYPE html>
<html>
<head>
    <script src="http://localhost:3000/socket.io/socket.io.js"></script>
    <title>Impact Game</title>
    <style type="text/css">
        html,body {
            background-color: #333;
            color: #fff;
            font-family: helvetica, arial, sans-serif;
            margin: 0;
            padding: 0;
            font-size: 12pt;
        }

        #canvas {
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
        }
    </style>

    <script src="lib/impact/impact.js"></script>
    <script src="lib/game/main.js"></script>
</head>
<body>
    <canvas id="canvas"></canvas>
</body>
</html>

当我转到 localhost:3000 时,我得到“SyntaxError: expected expression, got '

我尝试遵循的教程是使用 ImpactJS + NodeJS + Socket.IO 制作多人游戏,但这是迄今为止我所掌握的最远的。

这是main.js的内容

ig.module(
    'game.main'
)
.requires(
    'impact.game',
    'impact.font'
)
.defines(function(){

MyGame = ig.Game.extend({

    // Load a font
    font: new ig.Font( 'media/04b03.font.png' ),


    init: function() {
        // Initialize your game here; bind keys etc.
    },

    update: function() {
        // Update all entities and backgroundMaps
        this.parent();

        // Add your own, additional update code here
    },

    draw: function() {
        // Draw all entities and backgroundMaps
        this.parent();


        // Add your own drawing code here
        var x = ig.system.width/2,
            y = ig.system.height/2;

        this.font.draw( 'It Works!', x, y, ig.Font.ALIGN.CENTER );
    }
});


// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 320, 240, 2 );

});

【问题讨论】:

  • main.js的内容是什么?
  • 你有一个流浪括号...
  • 如果错误在main.js,那是你应该发布的文件。
  • 您是否尝试运行 main.js 而不是 index.js?
  • 当我在本地运行 index.html 时,它可以工作,所以该文件应该是好的。但是当我通过 Node 执行此操作时,我得到了错误。

标签: javascript node.js


【解决方案1】:

看看这个:

function handler(req, res) {
  fs.readFile(__dirname + '/index.html', function( err, data ) {

无论浏览器要求什么,你都会提供index.html的内容

所以当浏览器请求lib/impact/impact.jslib/impact/main.js时……你给它index.html的内容。

你需要注意什么是requested (req.url) 并返回正确的东西。

【讨论】:

  • 我不明白的是,我查看的教程是这样的,它工作得很好。 youtube.com/…
  • 我不会浏览所有视频来找出它未能足够清楚地解释的内容。我想它根本没有从 node.js 加载索引页面,只是留下了一些样板。
  • 我已经从视频中下载了源代码并尝试了相同的结果。虽然该视频已有 4 年的历史,所以这在当时是否可行但现在不行了?
  • 如果您将浏览器指向错误的 URL,使用相同的源代码将无济于事,这就是我想您正在做的事情。
  • 你是对的。我将 url 指向 :8080 而不是文件位于 wamp 上的实际文件夹。
猜你喜欢
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 2015-09-13
相关资源
最近更新 更多