Create express server without port - javascript

I would like to know if is possible create a server using express without serving any port. I need create a server just for socket connection with other application using TCP protocol.
Maybe express is not the best tool for that? Cause express is for web frameworks, and i don't really need ANY port at all.
Currently i'm doing:
http.createServer(app).listen(8000, function() {
console.log('Connected in port 8000');
});
Thanks.

If you're not going to have an http server listening for incoming HTTP connections, then you don't even need an Express server at all since it is a web server framework. No need to use the Express framework at all.
Instead, you can just use the built-in net module and use plain TCP socket functions in that module (see here for creating a plain TCP server). If you are going to be listening for incoming TCP connections, you will still need to be listening on a specific port - that's how TCP works. An incoming connection connects on a specific port and it must connect to a server listening on that specific port.

Related

express.js server starts on a port that is being used

Have this setup:
expressjs server started from one node process, listening to port 8081.
js client trying to make a request to it from another node process, using node's http module.
client failing with 400 error.
same URL (http://localhost:8081/) is opening in browser just fine.
Spent a few hours trying to troubleshoot it, then tried to change the port and it worked.
Turns out there is another process listening on port 8081:
$ lsof -i tcp:8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
EUSAManag 1187 oleksandr.suhak 4u IPv4 0xce3bb9546cff3ab1 0t0 TCP localhost:sunproxyadmin (LISTEN)
(Have no idea what EUSAManag is)
I guess my question is: how could it be that the express server starts fine without complaining to a "port being used by another process" when the port is clearly in use. And why does it work then when accessing it from the browser, but does not work when making request from js client? Any tips on figuring out what is actually happening here?
If guess that EUSAManager.exe come from one of software https://iit.com.ua/downloads which is in the business of information security. Most likely their software interfere too much with you operation system.

Connect to a remote socket.io server from Django server

I am trying to build a real-time Django application.
Because of the way my hosting service works, I am unable to start a Websocket server in parallel of my Django server.
I managed to have user-to-user interactions by creating an express server on a separate NodeJS website with socket.io, and having clients on the Django server also connect to the remote socket.io server.
However, I'dlike to have my Django server directly send events to users. To do this, I would like to create a connection between the Django server and the NodeJS server. Something like that in python:
socket = io("http://socket.io.server")
socket.emit('eventForUsers')
Is there anyway for me to achieve this?
The only information I found seemed to require me to run a parallel server from my Django app, which I can't do because my host doesn't allow me to run long-term processes.
It really depends what is the most simple solution for you, and what are your requirements. (If you want realtime bidirectional messaging then I suggest to use the socket.io-client instead of the POST example)
POST (GET,PUT,...)
You can (for example) use POST requests from your Django to Node
Django: python example (there are other ways to perform a POST to Node from Django)
reqParams = {"action":"doThis","data":"put the pizza in the oven"}
import requests
requests.post('http://ip:port/route', params = reqParams)
Node example : Listens for post at /route (express) and prints the params of the Django request
var express = require('express');
var app = express();
app.post('/route', function (req, res) {
console.log(req.query); res.end();
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Then you can use the data in req.params to perform action like broadcasting something to the socket.io clients (or a specific client)
This can also be done the other way around, performing requests to send data from Node to Django using POST (GET,...)
Socket.io-client in Django
Another (easier) solution is to include the socket.io-client in your Django webapp so it connects to your Node socket.io server as a client when the webapp is opened by browsers.
(using javascript in django)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://ip:port');
socket.on('connect', function(){alert("Hello World!");});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
</script>
More Resources :
1. JavaScript (Django Docs)
2. Including the static files in your template

Nodejs on VPS only running on my network

Nodejs server which is installed on my VPS is accessible only on my network. People from outside world cannot access it. If its online, it should either be accessible all over the world or nowhere. What to do?
Code in my js file:
var app = require('express')();
var http = require('http').Server(app);
// Also tried http.listen(3000, "0.0.0.0", function(){
http.listen(3000, function(){
console.log('Server listening to port 3000');
});
Well, in your question you say that you want the Node app to be accessible to everyone in the world, or nobody at all. If you're VPS provider restricts you to only running things on an internal network, however, then it is impossible to do what you are asking.
The network rules will simply not allow it.
With that said, however, I'm going to make a recommendation for changing your Express application. Here's how it should look:
let express = require('express');
let app = express();
app.listen(3000);
The code above will bind your Node application to port 3000 in the simplest way possible using Express directly. This is probably what you want.
Also: please note that if you are intending to build a public service, you will need to likely do one of two things:
Bind your Express server to port 80 (for HTTP), or
Use a web server to proxy requests from port 80 (HTTP) to port 3000 (local).
My bad adding the site's IP as the second parameter of listen function solved it.
http.listen(3000, "xx.xxx.xx.xxx", function(){
console.log('Server listening to port 3000');
});

Standalone socket.io and Websocket

I need to build a socket.io server that will intercept incoming connections from an app which is not stored in the same directory as the server.
The client side app does not contain node.js, thus I'm trying to use a websocket :
Telnet.Socket = new WebSocket('ws://127.0.0.1:3000');
My node.js server does not need a http server but must be a standalone socket.io app. Thus, I've tried the following code :
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('connexion entrante');
});
io.listen(3000);
Unfortunately, the server part does not seem to get the Websocket connection request. My firefox says :
Firefox cannot establish a connection with the server at adress ws://127.0.0.1:3000/.
What am I missing ?
Thx in advance !
socket.io need client use socket.io to connect because it use many kind of connection. For connect websocket only you can use ws node module

Node.js serve as a backend server for frontend app

I want to use Node.js run as a back-end server to serve front-end socket.io request.
The samples I found, seems can only run pages from Node.js server.
<script src="/socket.io/socket.io.js"></script>
The above js include is served by Node.js, how to include it if the front-end is a different application (e.g. PHP or static pages) ? Do I need to include all the dependent socket.io js libraries to make it work?
I'm currrently running apache/php alongside node.js/socket.io
If this is what you're trying to do, you can do it by serving socket.io on a different port than what apache is serving on (assumed 80).
In node.js, initialize the socket.io listener on a port 8080 for example:
var io = require('socket.io').listen(8080);
I believe, by default, socket.io will also serve its static client side files conveniently for you, such that in you html, you can:
<script src="http://yourhost:8080/socket.io/socket.io.js"></script>
Hope that helps.
It all depends on your server configuration. You may choose a path to Node.js backend that is not used by any other resource, and configure your web-server to serve everything else.
E.g. for Apache I used ProxyPass directive to enable connections to a local Node.js on a different port (to bypass local port restrictions), though may be not an option for your purposes:
ProxyPass /help/server/ http://localhost:8002/ connectiontimeout=1500ms

Categories

Resources