Using VHOST for subdomains on Express - node.js

I have 2 separated site (static web and application). I've tried to use Express vhost middleware but I couldn't manage.
For the below codes I configured my hosts file as;
127.0.0.1 localhost
127.0.0.1 process.localhost
my server.js codes
var connect = require('connect')
var express = require('express')
var vhost = require('vhost')
var app = require('./app')
var static = require('./static')
var server = connect()
server
.use(vhost('localhost', static.service))
.use(vhost('process.localhost', app.service))
.listen(1337, function(){
console.log('Server is listening')
})
Then if I write my address bar localhost:1337 static page comes this is good. However, if I write process.localhost:1337 nothing comes.
What should I do?
Edit
If I add the below middleware to my codes when I write the address bar localhost:1337 console write localhost:1337 but is I write process.localhost:1337 console write nothing.

Actually problem was the change of hosts file permissions.
Solution is here;
https://serverfault.com/a/452269/277517

Related

Nodejs SSL using CloudFlare not working over https

So the problem I'm having is that the client won't connect with the server.js when the server.js is using https.
if I go to "https://mydomainame.com" I get this error in the console of every other browser than brave browser
index.js:83 GET https://serverip:8081/socket.io/?EIO=3&transport=polling&t=NK0oCD6 net::ERR_CERT_AUTHORITY_INVALID
(The blacked out is the IP address of the server)
the weird thing is that in the brave browser the domain changes to "http://mydomainame.com" and the client then is connected to server.js
I'm using free Cloudflare with Full end to end encryption
server.js code:
var express = require('express'),
https = require('https');
var app = express();
var fs = require('fs');
var httpsOptions = {
key: fs.readFileSync('/var/www/ssl/sitename.com.key'),
cert: fs.readFileSync('/var/www/ssl/sitename.com.pem')};
var server = https.createServer(httpsOptions,app);
var io = require('socket.io').listen(server);
const port = 8081;
server.listen(port);
And client.js connection code:
socket = io.connect('https://serverip:8081', {secure: true});
I am using the same Origin Certificates for the server and for the nodejs code.
The server is using Apache2 with PHPMyAdmin and is configured to make the domain only work using https.
I read somewhere something Cloudflare not being able to use other ports than 443 and some other but I did not really understand it, And I can't get the server.js to work over port 443.
I'm thankful for any information or help I can get! :)
So I figured it out, big thanks to Eric Wong for pointing out the biggest problem that I was trying to connect to the server using its IP there for not going thru Cloudflare.
Then in this article Identifying network ports compatible with Cloudflare's proxy
you can see what ports Cloudflare allows connections on then, I changed my code to used the https port 8443.
socket = io.connect('https://domainname.com:8443',{secure: true});
then the only thing I had to do was to port forward the new port and everything worked fine!

404 - from nginx application to remote servers localhost?

I am new to react, and wanted to deploy a site to my domain with Nginx. I need to make the application to be able to fetch from client side, to the localhost of the remote server hosting the site with Nginx. I know exposing this many details might make security experts and hackers either drool or shake their heads. But I am losing my sanity from this.
This is a filtered version of my Node.js express service running on the remote server:
const express = require("express")
const cors = require("cors")
const app = express();
const PORT = 1234;
const spawn = require("child_process").spawn;
app.use(cors())
app.listen(PORT, function(){
console.log(`listening on port:${PORT}...`)
})
app.get("/api/play/:choice", function(req,res){
pythonProcess = spawn('python',["./script.py", req.params.choice]);
pythonProcess.stdout.on('data', function(data) {
res.status(200).send(data.toString('utf-8'))})
})
this is how I am fetching from the deployed react application. The public IP of the droplet I am using
fetch(`104.248.28.88/1234/api/play/rock`)
Change the fetch to replace the / with a : to indicate port, rather than directory
fetch("104.248.28.88:1234/api/play/rock")

Server responds with 404 when trying to connect Socket.io to an Express app

I'm building a VueJS app using vue-cli's webpack template.
I've split the front and back ends into different Heroku applications and deployed them.
Background:
My client app has the same setup as described here
tl;dr the above Medium article:
We now have a fresh Vue-cli/webpack app, and a server.js file used to create an Express server that serves the built app files.
The problem:
I've been running into issues trying to use socket.io on said server.js file.
Here's how server.js looks like:
var http = require('http'),
path = require('path'),
express = require('express'),
app = express(),
server = http.createServer(app),
socketIO = require('socket.io'),
port = process.env.PORT || 8080,
history = require('connect-history-api-fallback'),
serveStatic = require('serve-static')
app.use(serveStatic(path.join(__dirname, '/dist')))
server.listen(port, () => {
// logs when running node server.js
console.log('listening on port', port)
})
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('Connected!!!');
});
And this is how I call socket.io inside of my .vue component:
const io = require('socket.io-client')
const socket = io('http://localhost:8080')
As soon as this last line is uncommented I receive a friendly Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/socket.io/?EIO=3&transport=polling&t=M9yJX8nsocket.io/?EIO=3&transport=polling&t=M9yHe0F
Additional info:
Not sure if relevant, but still saying - I'm Using "connect-history-api-fallback" to point all non-existent routes to a wildcard 404 .vue component that displays a friendly user message and allows them to go back to existing routes.
Can this be a part of the reason? What I read about my issue is that I probably have trouble making socket.io run on the same server that my app is running in.
I experienced the problem initially when trying to first connect my Vue App with Vue-Socket.io
upon the line
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));
Where as an URL I used http://localhost:8080
I've spend a good few days on the problem and still have no clarity on where this problem is rooted in. I am really trying to understand and would highly appreciate any form of feedback. I read about people having the same / similar problem, and tried calling io() without my localhost + port url.
First question here, hope it's properly asked.
Okay, guys, I don't know why, but following the code placed in the "Docs" section of Socket IO's website resulted in the same error (Express 3/4 section). I then went on to copy the Chat demo and had success, so I'm now importing Socket.IO in my Index.html file
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
With server.js looking like this:
var path = require('path'),
app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http),
port = process.env.PORT || 8080,
history = require('connect-history-api-fallback'),
serveStatic = require('serve-static')
app.use(serveStatic(path.join(__dirname, '/dist')))
http.listen(port, () => {
console.log('listening on port', port)
})
io.on('connection', (socket) => {
console.log('Connected!!!');
});
I'm going to close this now and see how to implement the functionality I'm looking for - I now have a good starting point. Best of luck to anyone struggling with this.
Cheers!

Socket.io, server not responding to connection event

I have a managed server at cloudways. I am trying to use socket.io.
client.php:
var socket = io('http://localhost:3000');
console.log(socket);
server.js:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
io.on('connection', function(socket){
console.log('is connected');
});
When visiting the page, I see two instances of 200 OK in the network section of the console.
The console in the browser also prints the socket variable, which has "connected : true".
However, I DO NOT see "is connected" in the terminal (from server.js), it is just blank.
This site has worked locally, but these problems are appearing when trying to get it to work at Cloudways.
Any ideas?
I have tried switching "localhost" to both the domain name and the IP-address, and I have tried different ports, but 3000 is the only one that will let me start the server.js without error.
EDIT
Cloudways have confirmed that port 3000 should be used.
I have tried changing the url in io() to:
var socket = io('http://my-cloudways-url:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://www.my-domain.club:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://ip-adr:3000'); (net::ERR_EMPTY_RESPONSE)
And today I am getting
var socket = io('http://localhost:3000'); (net::ERR_CONNECTION_REFUSED)
I have also tried io.connect(using the same adresses / ports).
Maybe is your php index... Because you are setting the localhost and the port.
I'm not sure about how Cloudway works. But, when we run the server.js, some clouds give some url for access our projects...
In your cloudways try to use the official URL that they given to you access the project:
var socket = io.connect('url.from.cloudway');
For example, I'm using cloud9, and when I execute the server.js, return one url like:
Your code is running: https://demo-some-id.c9.io
And when I'm using socket.io, I need to set in my index the URL parameter inside the IO.
var socket = io('https://demo-some-id.c9.io');
You need to add the client-side socket.io.js version and add the script tag with the path, like:
<script src="js/socket.io.js"></script>
Obs.: Some clouds, you need to set the url in your <script> tag
<script src="https://demo-some-id.c9.io/socket.io/socket.io.js"></script>

Accessing local nodejs server from salesforce.com

I have set up a local server in my machine using nodejs. something like this:
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.get("/",function(request,response){
response.send('thanks for visiting my site');
});
app.listen(port, host);
Now, If I send a http request to this server, I get the 502 seervice unavailable response.
HttpRequest req = new HttpRequest();
req.setEndpoint('http://localhost:4555/');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
I have also added the localhost url in the remote site setting, but still I am unable make any calls. Any idea how to go about it?
localhost is a relative address, which machine it resolves to depend on where it used, so when you use localhost in apex code (which is running on a salesforce server), its trying to talk to itself not your nodejs host. You need to give your host a publicly resolvable name or IP address and use that in your apex code.

Resources