I've been playing with nodejs, using websockets to communicate between server and browser.
This is my working client code:
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script>
var socket = io();
socket.on('ida',function(data){
console.log(data.news);
socket.emit('vuelta',{news: 'answer'});
});
</script>
Then why if I put
var socket = io();
socket.on('ida',function(data){
console.log(data.news);
socket.emit('vuelta',{news: 'answer'});
});
in a separated client.js file and link it like
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script src="client.js" type="text/javascript"></script>
it just doesn't work?
In the browser's console, instead of showing data.news it says: Uncaught SyntaxError: Unexpected token < in client.js:1
It might be that you are not serving the file at all. Be careful to include it in the http server configuration, on your node server file.
Related
I'm starting with Node.js and I have already a problem in my first program. Below is the code I'm using. Index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Random Temperatures</title>
</head>
<body>
<input type="text" id="tb" name="tb" />
<input type="button" value="Random Number!" id="myButton" name="myButton"/>
<script src="client.js"></script>
</body>
</html>
Client.js:
const textBox = document.getElementById('tb');
const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
var rnd = Math.floor(Math.random() * 100);
textBox.value = rnd;
});
Server.js:
var app = require('http').createServer(response);
var fs = require('fs');
app.listen(8080);
console.log("App running…");
function response(req, res) {
fs.readFile(__dirname + '/public/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Failed to load file index.html');
}
res.writeHead(200);
res.end(data);
});
}
When I start the application I go to the browser the text box and the button appear. But in the browser console I'm getting these errors:
client.js:1 Uncaught SyntaxError: Unexpected token <
ContentScript.js:112 Exception in onResRdy: TypeError: Cannot read
property 'htmlRes' of undefined
localhost/:1 Unchecked runtime.lastError: Could not establish
connection. Receiving end does not exist.
I guess my problem is the linking between the 3 files but I tried several things and I can't solve the problem. I'm sure it's a stupid error but forgive me I'm just getting start. Any advice?
The browser (because you have <script src="/client.js">) makes a request for /client.js
The server:
Gets the request
Runs response
Reads index.html
Sends it to the browser
Since index.html starts with <, the browser throws an error when it tries to run it as JavaScript.
Why are you giving the browser index.html when it asks for client.js?
You need to examine the request object, determine what URL is being asked for, write logic to return the correct resource with the correct status code and the correct content-type, and then return that to the client.
The Node.js documentation has an example of this but you should probably stop trying to use createServer directly — since it involves a massive amount of wheel reinvention — switch to using Express and work through the (very short) getting started guide which includes a section on using the static module to serve up static files.
I'm starting with Node.js and I have already a problem in my first program. Below is the code I'm using. Index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Random Temperatures</title>
</head>
<body>
<input type="text" id="tb" name="tb" />
<input type="button" value="Random Number!" id="myButton" name="myButton"/>
<script src="client.js"></script>
</body>
</html>
Client.js:
const textBox = document.getElementById('tb');
const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
var rnd = Math.floor(Math.random() * 100);
textBox.value = rnd;
});
Server.js:
var app = require('http').createServer(response);
var fs = require('fs');
app.listen(8080);
console.log("App running…");
function response(req, res) {
fs.readFile(__dirname + '/public/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Failed to load file index.html');
}
res.writeHead(200);
res.end(data);
});
}
When I start the application I go to the browser the text box and the button appear. But in the browser console I'm getting these errors:
client.js:1 Uncaught SyntaxError: Unexpected token <
ContentScript.js:112 Exception in onResRdy: TypeError: Cannot read
property 'htmlRes' of undefined
localhost/:1 Unchecked runtime.lastError: Could not establish
connection. Receiving end does not exist.
I guess my problem is the linking between the 3 files but I tried several things and I can't solve the problem. I'm sure it's a stupid error but forgive me I'm just getting start. Any advice?
The browser (because you have <script src="/client.js">) makes a request for /client.js
The server:
Gets the request
Runs response
Reads index.html
Sends it to the browser
Since index.html starts with <, the browser throws an error when it tries to run it as JavaScript.
Why are you giving the browser index.html when it asks for client.js?
You need to examine the request object, determine what URL is being asked for, write logic to return the correct resource with the correct status code and the correct content-type, and then return that to the client.
The Node.js documentation has an example of this but you should probably stop trying to use createServer directly — since it involves a massive amount of wheel reinvention — switch to using Express and work through the (very short) getting started guide which includes a section on using the static module to serve up static files.
I'm starting with Node.js and I have already a problem in my first program. Below is the code I'm using. Index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Random Temperatures</title>
</head>
<body>
<input type="text" id="tb" name="tb" />
<input type="button" value="Random Number!" id="myButton" name="myButton"/>
<script src="client.js"></script>
</body>
</html>
Client.js:
const textBox = document.getElementById('tb');
const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
var rnd = Math.floor(Math.random() * 100);
textBox.value = rnd;
});
Server.js:
var app = require('http').createServer(response);
var fs = require('fs');
app.listen(8080);
console.log("App running…");
function response(req, res) {
fs.readFile(__dirname + '/public/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Failed to load file index.html');
}
res.writeHead(200);
res.end(data);
});
}
When I start the application I go to the browser the text box and the button appear. But in the browser console I'm getting these errors:
client.js:1 Uncaught SyntaxError: Unexpected token <
ContentScript.js:112 Exception in onResRdy: TypeError: Cannot read
property 'htmlRes' of undefined
localhost/:1 Unchecked runtime.lastError: Could not establish
connection. Receiving end does not exist.
I guess my problem is the linking between the 3 files but I tried several things and I can't solve the problem. I'm sure it's a stupid error but forgive me I'm just getting start. Any advice?
The browser (because you have <script src="/client.js">) makes a request for /client.js
The server:
Gets the request
Runs response
Reads index.html
Sends it to the browser
Since index.html starts with <, the browser throws an error when it tries to run it as JavaScript.
Why are you giving the browser index.html when it asks for client.js?
You need to examine the request object, determine what URL is being asked for, write logic to return the correct resource with the correct status code and the correct content-type, and then return that to the client.
The Node.js documentation has an example of this but you should probably stop trying to use createServer directly — since it involves a massive amount of wheel reinvention — switch to using Express and work through the (very short) getting started guide which includes a section on using the static module to serve up static files.
Here is my code :
<html>
<body>
<script src="bin/phantomjs"></script>
<script src="js/test.js"></script>
</body>
</html>
inside test.js
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('http://www.httpuseragent.org', function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var ua = page.evaluate(function () {
return document.getElementById('myagent').innerText;
});
console.log(ua);
}
phantom.exit();
});
I got these error when viewed using Chrome.
Uncaught SyntaxError: Unexpected token ILLEGAL
test.js:1 Uncaught ReferenceError: require is not defined
I wanted to load PhantomJS in my html project, so that I can begin to scrape websites.
PhantomJS is a standalone application/browser that takes a script written in JavaScript and drives a headless browser component. You can't load that script into a browser and drive either PhantomJS or the browser it is executed in with that script. It is comparable with node.js in that you can't simply run node.js scripts in the browser. Note that node.js and PhantomJS have different execution environments.
If you want to trigger a PhantomJS script from a client-browser, you would make an HTTP request from the client browser to your server, where you would call the PhantomJS script through exec() or something similar in your favorite server framework/language, compute the result on the server and send it back to the client.
i know there's been couple of question about the same problem , i've already check them .
i have very simple node.js chat app
i have a server running on 8000 port and it works fine
my client pages are html , they are running on apache and i'm using socket.io to connect them to the server and it works fine on the local host
but when i upload the app on the server i keep on getting this error in the firebug
io is not defined
var socket = io.connect('http://atenak.com:8000/');
or sometimes it doesn't show that but when i try to broadcast message from cliend i get this error :
socket is undefined
socket.emit('msg', { data: msg , user:'max' });
the only difference is i've changed the localhost with atenak.com !
here is my html code
var socket = io.connect('http://atenak.com:8000/');
var user = 'jack';
socket.on('newmsg', function (data) {
if(data.user == user )
{
$('#container').html(data.data);
}
});
function brodcast(){
var msg = $('#fild').val();
socket.emit('msg', { data: msg , user:'max' });
}
</script>
</head>
<body>
<div id="container"> </div>
<input id="fild" type="text"> <input name="" type="button" onClick="brodcast();">
</body>
i have included the sockt.io.js
src="http://atenak.com:8000/socket.io/socket.io.js"
and server is running ok which means socket.io is installed on the server
here is the live page
http://atenak.com/client.html
I'm getting:
> Uncaught ReferenceError: io is not defined client.html:7
x GET http://atenak.com:8000/socket.io/socket.io.js
indicating that your page cannot load http://atenak.com:8000/socket.io/socket.io.js; indeed, if you try to load that URL in the browser, you get a connection error. Make sure that your Node.js server (running Socket.IO) is accessible (e.g. ports not being blocked by a firewall, etc).
Seems port 8000 is not open on your host as per links below
Reverse ip check for atenak.com and Open port check
Socket.io need a javascript file and this file does not load correctly.
Here is the url: http://atenak.com:8000/socket.io/socket.io.js
In this file is defined the îo object.