I am using MAMP server and node js.
My issue is that I want to run apache server and node on the same port, but solutions online are quite complicated as it does not server much of the purpose, also I am new to Node. I need a simple solution where I can node on the port of apache server.
Code is below,
it works fine, as everything is running on different port but I want the 'server' to run on port 8888 where apache is running. Or some way for node to interact with apache
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var mysql = require('mysql');
var router = express.Router();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'db',
port : '8889'
});
server.listen(8888); <----- ERROR
console.log('server running.......');
connection.connect(function(error){
if(!!error) {
console.log('error in db connection');
} else {
console.log('db connected');
}
});
app.use('/*', router);
router.get('/*', function (req, res) {
console.log('hello there...');
});
I think the proper way to do this is to 'face' the Node service using Apache's Proxypass. This will forward a path to the node service running on a different port.
Adding the following to a your httpd.conf or under a vhost and restarting Apache should give you a working result
#I'm using 8890 for Node since your Apache runs on 8888 and DB is on 8889
ProxyPass /node/ http://localhost:8890/
ProxyPassReverse /node/ http://localhost:8890/
Navigating to http://localhost:8888/node/ will connect you to the node service running on port 8890.
You can use http-proxy for that.
var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
hostnameOnly: true,
router: {
'127.0.0.1': '127.0.0.1:8080'
}
});
proxyServer.listen(8888);
This will create a node process listening to port 8888, and forwarding requested domains to 8080.
you will find many tutorials on this topic but here is a overview:
you need apache mod mod_proxy
basicly setup you need something like this, this link includes insturctions on how to set it up in a way, it will also forward your websockets (if you need that).
you will use somethink like, http://localhost:3005 where 3005 is the port nodejs runs on. (choose whatever port you like)
it might be usefull to add this port to you iptables to prevent direct access (without apache)