I'm fairly new to nodeJS/Express which I'm learning at the moment.
There seems to be different methods of creating a http server and I'm wondering what the difference is. E.g.....
From a socket.io tutorial:
var app = require('express')();
var http = require('http').Server(app);
...and from a nodejs tutorial:
var express = require('express'),
app = express.createServer();
Can someone explain the difference between the two, particularly in regards to the first example? I'm presuming the empty brackets after the express require is an anonymous function, but what is that performing? Why pass the app to the Server method?
Related
I just started to study nodejs and express, and was trying to make a simple router. In the beginning it worked, if I remember correctly, but since I reopened the project it hasn't worked anymore.
var express = require('express');
var app = express();
var route = express.Router();
route.get('/',(req,res)=>{
res.send('Something');
})
app.get('/post',route);
app.listen(3000);
When I make a GET to 127.0.0.1:3000/post I expect "Something" in my response body, instead I get the default node 404.
The problem is that the path is not "cut", in fact it works if I use route.get('/post',...;
from what I have seen in many examples, and from the documentation itself, when a router is invoked, the path is "cut", am I wrong?
For now I have tried to reinstall express, thanks in advance for your answers. I searched around and couldn't find any other similar problems.
It's because you didn't register the middleware (use is a way to register middleware) and you don't need two endpoints ('/'). your code runs perfectly now:
var express = require('express');
var app = express();
var route = express.Router();
route.get('/post',(req,res)=>{
res.send('Something');
})
app.use(route);
app.listen(3000);
As jfriend00 pointed out to me in the comments, the mistake is to use .METHOD() to invoke the route, rather than .use() .
I am new to SocketIO, I have referred many blogs and documentation for socket and everywhere we first need to create an HTTP server and then attach the socket to it like this -
var app = express();
var httpServer = http.createServer(app);
var io = socketio.listen(httpServer);
What does the second line mean? why are we creating one extra HTTP server while express(web framework) is already defined?
Because I never created a new HTTP instance for my RESTful application, I simply listened to express instance like this -
var express = require('express');
var app = express();
app.listen(8000);
Thanks in advance!
If you want socket.io to run on the same port as your web server, then you use the same server instance. If you want socket.io to run on a different port, then you create a new server instance on that port just for socket.io to use.
Socket.io works just fine using the same port and server instance as Express so unless you have a specific reason to run it on a different port, this is the usual way one would configure it.
Some code examples for socket.io show it in isolation by itself and thus they have to create an http server for it to use.
When using Express, you can get the server instance like this:
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = app.listen(8000);
const io = socketio(server);
I am pretty new to node.js and now I am doing a project on building a website on node.js. Sorry if my question is very naive.
I am using express framework.
My app.js is listening at port (3000).
In my route.js, I got some data from calling some API. I want to display the data to my datapoint.jade file. And because I want to draw those datapoint I have to embed a javascript file in jade. I want to use socket.io to achieve this sending and receiving data.
However, all the examples to construct socket is in app.js. I get data in router.js(in one of its callback function). app.js require router.js.
How can I send data from route.js to app.js and then send out to client side.
Could you please guide me some related and useful information? Or my design would not work at all?
A common paradigm is to pass any dependencies to your child modules:
// app.js
var socket = require('socket.io');
var app = require('express')();
var routes = require('./routes.js')(app, socket);
app.listen();
// routes.js
module.exports = function(app, socket){
var routes = {};
app.use('/', routes.handleIndex)
socket.on('connection', function(){
...
})
}
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'm working on a project which consists in creating a game of the goose like. In order to do that, I'm using Node.js, Express, jade and now Socket.io. But I encounter some trouble, like, in example, to share the position of one client to the other client. Because my variable position is in a function in index.js and I don't know how I can use Socket.io in a route file. I try some things, but nothing works.
On internet, I've seen some people who say that there is no-sense to use Socket.io in an express route file. So how can I do that ?
In my index.js I've that :
exports.deplacement = function(io)
{
return function(req,res)
{
//[...]
io.sockets.on('connection', function(socket)
{
socket.broadcast.emit('position', space);
});
res.render('moteur' //[...]);
}
}
And in my moteur.jade I've done this :
script(src="/socket.io/socket.io.js")
script.
var socket = io.connect('http://localhost:3000');
socket.on('position ', function(space) {
alert(space);
})
First of all, I'm not sure what your question exactly means, but if it is what I think it is then I think what you mean by using socket.io in a route file is to be able to include the client side javascript lib provided with socket.io module of Node.
In order to do that, you have to allow the socket.io module to listen to server. This works like a middle-ware itself. Everything has to go through socket.io first before they are routed to the server. So, when you request the client side lib, it is uploaded to the client.
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server)