Node.js on a remote server: io is not defined - javascript

I'm building an application that links to a node.js hosted on my home computer (78.233.79.103:8000)
The server is properly istalled (if you go to the addres I gave you'll se the socket.io works)
If I run the server in localhost with
node server.js
all is good, the application works
Then when I run the application on my other pc or on my iPad (I wrapped it with phoneGap so it's just a web app included in a native iOS app), trying to connect the io on 78.233.79.103:8000 i got the console log:
io is not defined
here is my sourcecode: https://github.com/synbioz/puissance4
look for the server.js
I know I only call io = require('socket.io').listen(PORT); but kwnow also I should create something like:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, '78.233.79.103');
that actually doesn't works
Any idea?

probably because .listen() doesn't return this.
try writing them in separate lines.
var io = require('socket.io');
io.listen(PORT);

Related

create a web server for local html using node

I'm tring to create a simple web server to run my local html file, and now I can get a server running on localhost using code below
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html', 'utf8');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'html'});
res.end(index);
}).listen(3000);
But there is a problem that every time I changed my html file, the web page on localhost is still the old one, I need to restart the server to make the changes visible.
I want to improve that and I tried to use koa or express, like code below
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Application started and Listening on port 3000");
});
app.get("/", (req, res) => {
res.sendFile("test.html");
});
And I found that express can serve real-time html files, I don't need to restart serve when files change.
How can I use http moudle to achieve that?
Currently you read the file when the program starts up. Then each time you get a request you serve up the data from the variable.
Move that code inside the callback function that runs when you get a request, then it will be updated each time a request comes in.
http.createServer(function (req, res) {
var index = fs.readFileSync('index.html', 'utf8');
res.writeHead(200, {'Content-Type': 'html'});
res.end(index);
}).listen(3000);
You could avoid reading the file on each request by having the variable outside the function (as in your original code) and watching the file for changes (and updating the variable in response).
That said, using Express and its static module will provide benefits with caching and I'd recommend it over rolling your own.
You can use nodemon.
npm install -g nodemon
nodemon server.js

Express.js connection refused when accessing website from browser

Im trying to access my node.js server from my browser, as i want to use websockets later, but im getting an
ERR_CONNECTION_REFUSED
on the url: "http://my-website.com:3000".
Connecting to "http://my-website.com" works fine.
My app.js:
var app = require("express")();
var server = require("http").Server(app);
app.get("/", function(req, res) {
res.send("");
});
server.listen(3000);
I just started with node.js and express.js today, is this normal behaviour?
Thanks for any help in advance!

How to run a website developed with node.js in local?

I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on nodejs.org:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.

making http server with node and browserify

im making a mobile app that require a http server so im trying to make it with browserify so this is what im doing, i got this server.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
then i do this
$ browserify server.js > browserify.js
then i enmbeded to my html like this
<html>
<body>
<script src="https.js"></script>
</body>
</html>
then when i run the html i get this error
Uncaught TypeError: http.createServer is not a function
how can i get a http server working on the browser ?
by the way i got http-browserify installed
Apologies if I'm reading this question wrong, but you can't create an http server from within a browser. That http.createServer call is only meant to be called (and is only defined) in node.js.
So you would need to run your script from node from the command line, like this:
node https.js

How to run multiple Node.js application while in development

I am trying to run two node.js application in my development machine but the second application throwing the following exception while running.
Is there any way to run these two applications in parallel way ?
You need to use a different port since 3000 is already in use.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3001, "127.0.0.1");
Address is already in use, you should change the port from for example 3000 to 3001 for the second instance of the script.
You can't bind two sockets on the same port. Hence the error.
The usual good practice is to rely on the PORT environment variable so as to be able to quickly change the listening port from the command line.
var http = require('http');
var port = process.env.PORT || 3000;
http.createServer().listen(port);
Then launch your application:
$ PORT=8080 node app.js
See here for cross-platform instructions on how to define environment variables from the command line.

Categories

Resources