I don't understand the javascript error.

Asked 2 years ago, Updated 2 years ago, 77 views

There is an error in client-ios.js, but I don't know where to fix it.
It seems to be an error with the symbol <, but I don't use it anywhere, so it's refreshing.
If you are familiar with javascript, please let me know

Error Contents

SyntaxError: expected expression, got'<'client.js:1

http://d.hatena.ne.jp/tomo-ono/20110530/1306740692
I created it by referring to the site, but I don't know how to solve it

Source code for the project created using the above URL

client.js

$(function(){
var socket = newio.Socket(null, {port:8080});
socket.connect();

function iosHandleOrientation(event){
    variableData=event.accelerationIncludingGravity;
    socket.send(orientData.x+"+orientData.y+"+orientData.z);    
}

window.addEventListener("devicemotion",iosHandleOrientation, true);

socket.on('message', function(obj){
    data=obj.split("";
    $('#data') .append("<tr><td>"+parseFloat(data[0]).toFixed(3)+"</td><td>"+parseFloat(data[1]).toFixed(3)+"<td>td>2Float;"

});
});

server.js

varfs=require('fs');
var http=require('http'),
    io= require ('socket.io'),

server = http.createServer(function(req,res){
  fs.readFile('./socket.html', 'utf-8', doRead);
  function doReard(err,data){
    res.writeHead(200, {'Content-Type':'text/html'});
  res.write(data);
    res.end();
}
});
server.listen (8080);

var socket =io.listen(server);
socket.on('connection', function(client){
    client.on('message', function(message){
        client.broadcast(message);
        console.log(message);
    });

    client.on('disconnect', function(){
            console.log('Connection closed.');
    });
});

socket.html

<!DOCTYPE html>
<html lang="ja">
<metacharset="utf-8">
<head>
     <script type="text/javascript" src="https://cdn.socket.io/socket.io- 1.4.5.js">/script>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">/script>
     <script src="client.js"></script>
</head>
<body>
<table id="data">
<tr>th>x>/th>y>/th>th>z>/th>>
</table>
</body>
</html>

As you pointed out, I rewritten the server.js and socket.html codes, but I got the following error

TypeError: server.use is not a function

server.js

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

server = http.createServer(function(req,res){

  server.use(express.static(__dirname+'/public'));
  //fs.readFile('./socket.html', 'utf-8', doRead);
  function doReard(err,data){
    res.writeHead(200, {'Content-Type':'text/html'});
  res.write(data);
    res.end();
  }
});
 server.listen (8080);

var socket =io.listen(server);
socket.on('connection', function(client){
    client.on('message', function(message){
        client.broadcast(message);
        console.log(message);
    });

    client.on('disconnect', function(){
            console.log('Connection closed.');
    });
});

socket.html

<!DOCTYPE html>
<html lang="ja">
<metacharset="utf-8">
<head>
    <script type="text/javascript" src="https://cdn.socket.io/socket.io- 1.4.5.js">/script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">/script>
    <script type="text/javascript" src="http://localhost:8080/images/client.js"></script>
</head>
<body>
<table id="data">
<tr>th>x>/th>y>/th>th>z>/th>>
</table>
</body>
</html>

I'm sorry to keep bothering you, but if you know anything about it, please take care of me

javascript html node.js iphone socket.io

2022-09-30 14:36

2 Answers

(This answer was originally posted on Duplicate destination question, but I also posted it here because it was closed as a duplicate.)

This may be because server.js returns socket.html regardless of the path of the request.

The browser looks at the tag <script src="client.js">/script> and requests to the server, but I think the error occurred because it was returned by html.

Use Express for ease of routing.

Related Resources
Hosting Static Files (static Files) in Express


2022-09-30 14:36

There was a similar error on our English website and a solution.
https://stackoverflow.com/questions/31926326/syntaxerror-expected-expression-got-what-does-that-mean

Does it not match this?

You don't need the tags when in an external.js file.Use these tags to embedded a script inside HTML only.

The answer is
in Japanese. "External JS files do not require tags.Tags are only required when embedding scripts in HTML."

That's right.


2022-09-30 14:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.