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!
Related
I'm trying to create a server using Node.js and socket.io and it starts perfectly. However, when I'm trying to visit the site of my server through the browser, he writes "Cannot Get /" and in the console gives an error "Failed to Load Resource: The Server Respondd with A Status of 404 (Not Found)". For two days I'm trying to understand where the problem and I will be very grateful to your help. Here is my server code:
const express = require("express");
var http = require("http");
const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = require("socket.io").listen(server);
//middlewre
app.use(express.json());
io.on("connection", (socket) => {
console.log("connected");
console.log(socket.id, "has joined");
socket.on("/test", (msg) => {
console.log(msg);
})
});
server.listen(port, "0.0.0.0", () => {
console.log("server started");
});
Your server is currently not set up to handle any other http traffic than Websocket connection upgrade requests, which you can not make by entering a url in your browser. Also, the browser is not equipped to negotiate this connection upgrade or to keep the Websocket connection working once established. For this you need some javascript code to run on the client side.
The socket.io library provides everything you need, look at a minimally working example at the link provided below. You should basically just set up your server to serve an html document which provides a context from which the whole websocket connection upgrade can be managed - and the connection maintained - by the socket.io library.
https://socket.io/docs/v2#Minimal-working-example
I've just started looking at node.js so forgive if i'm wrong.
I'm creating a basic chat application using this tutorial with socket.io - Link
I've set it up and its running on local host but how can i get this to work so it would connect to my website address http:// mywebite.co.uk on a port
so http:// mywebite.co.uk/:101
Any ideas sorry if this is a basic question ?
This is my code
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
I am quite confused about setting up a secure ssl connection on node.js and the bindings created with the socket.io.
It is giving big headaches, as it is constantly refusing to work, and the result in browsers tested are:
GET https://webrtc.romlex.info/socket.io/1/?t=1405428623632 404 (Not Found) socket error
My serverside code:
var options = {
key: fs.readFileSync(__dirname + '/public/video-phone/key.pem'),
cert: fs.readFileSync(__dirname + '/public/video-phone/cert.pem')
};
actually i am only initializing express
var app = express();
should i create server when initializing express, and pass options?
var app = express.createServer(options);
actually i am creating the https server with the options listening on secured port
var server = require('https').createServer(options,app).listen(443);
which way should i initialize socket.io connection?
Actually i am listening on server with options
var io = require('socket.io').listen(server,options);
Should i listen on server only?
var io = require('socket.io').listen(server);
Or should i listen on the secure port and options?
var io = require('socket.io').listen(443,options);
Quite a bit confused, please help!
i am using xhr-polling over websockets
io.set('transports', [
//'websocket',
'xhr-polling',
'jsonp-polling'
]);
the serverside code:
serverside socket.io with ssl
clientside code:
should i add secure param or not?
io.connect("https://webrtc.romlex.info", {secure: true});
Sorry for the questions, i'm quite a newbie on node.js, especially, when ssl connection comes to scene.
Thanks for your suggestions!
I have an android app that is going to send a json to my server where I will have a node.js express app. Meanwhile, I want to test it on my localhost.
On my android code I send the json to:
new HttpAsyncTask().execute("http://10.0.2.2:8080/ReceiveJson");
This code is triggered by a button and is working fine.
Then in my app.js file I have the following code:
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 8080);
app.get('/ReceiveJson', function(req, res) {
console.log(req.body);
res.send(req.body);
res.json(req.body);
res.send("ok");
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
In my terminal I run node app.js and I get just the answer "Express server is listening on port 8080". Nothing else.
When I go to my browser and put
http://localhost:8080/ReceiveJson
I get "{}" as an answer.
In my terminal I get the same answer.
What do I have to do to receive the json?
Thanks
That's because you are responding with the req.body:
res.send(req.body);
The code below it never gets called because you have already responded:
res.json(req.body);
res.send("ok");
Because you are using a web browser, there is nothing being sent in the body because it is a get request and there will be nothing in the body for a get request, therefore you get an empty json object, hence {}.
If you change your code to:
app.get('/ReceiveJson', function(req, res) {
res.send("ok");
});
Then when you browse there in your browser you will get the response 'ok'
If you are looking to post or put to your express server then you need to use either app.post or app.put. I noticed that you are trying to send JSON with an Android device for an app you have already written. I would highly recommend using Fiddler or something to test with, just make sure that when you send JSON to your express app you are using the header:
Content-Type: application/json
EDIT:
Your JSON might not be working because you aren't using the body parser. Try inserting this in your code before app.listen
app.configure(function(){
app.use(express.bodyParser());
})
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);