NodeJS HTTP - listen on other port than 80
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am running XAMPP on Windows to host an Apache server on port 80. Now I'm trying to have a NodeJS script running in the background but the problem is that it can only listen on port 80. If it does, everything works as it should but I can't have Apache running at the same time, because Apache takes priority and just serves my website. The NodeJS script doesn't even get to listen.
My question is: how do I switch the listening port of the NodeJS script (the specific port really doesn't matter) so that Apache can still run on port 80 and I can reach the NodeJS script from all around the world.
Part of the NodeJS code:
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});Additional information is in my other question which got flagged as duplicate.
node.js apache http xampp
add a comment |
I am running XAMPP on Windows to host an Apache server on port 80. Now I'm trying to have a NodeJS script running in the background but the problem is that it can only listen on port 80. If it does, everything works as it should but I can't have Apache running at the same time, because Apache takes priority and just serves my website. The NodeJS script doesn't even get to listen.
My question is: how do I switch the listening port of the NodeJS script (the specific port really doesn't matter) so that Apache can still run on port 80 and I can reach the NodeJS script from all around the world.
Part of the NodeJS code:
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});Additional information is in my other question which got flagged as duplicate.
node.js apache http xampp
1
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
1
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43
add a comment |
I am running XAMPP on Windows to host an Apache server on port 80. Now I'm trying to have a NodeJS script running in the background but the problem is that it can only listen on port 80. If it does, everything works as it should but I can't have Apache running at the same time, because Apache takes priority and just serves my website. The NodeJS script doesn't even get to listen.
My question is: how do I switch the listening port of the NodeJS script (the specific port really doesn't matter) so that Apache can still run on port 80 and I can reach the NodeJS script from all around the world.
Part of the NodeJS code:
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});Additional information is in my other question which got flagged as duplicate.
node.js apache http xampp
I am running XAMPP on Windows to host an Apache server on port 80. Now I'm trying to have a NodeJS script running in the background but the problem is that it can only listen on port 80. If it does, everything works as it should but I can't have Apache running at the same time, because Apache takes priority and just serves my website. The NodeJS script doesn't even get to listen.
My question is: how do I switch the listening port of the NodeJS script (the specific port really doesn't matter) so that Apache can still run on port 80 and I can reach the NodeJS script from all around the world.
Part of the NodeJS code:
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});Additional information is in my other question which got flagged as duplicate.
const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});const http = require('http');
const port = 8080;
const host = '0.0.0.0';
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body);
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})
server.listen(port, null, function(error){
if(!!error){
console.log("x1b[41m%sx1b[0m", "error while initializing listener on port " + port + ": " + error);
}
else{
console.log("x1b[32m%sx1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
});node.js apache http xampp
node.js apache http xampp
edited Jan 30 at 10:58
Sv443
asked Oct 22 '18 at 9:20
Sv443Sv443
355217
355217
1
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
1
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43
add a comment |
1
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
1
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43
1
1
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
1
1
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43
add a comment |
6 Answers
6
active
oldest
votes
Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:
I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.
This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:
I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:
+---------+------+-------------------------------------+
| Backend | Port | Web Address |
+=========+======+=====================================+
| Apache | 80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1 | 8080 | 100.120.110.43:8080 |
| Node2 | 8081 | 100.120.110.43:8081 |
+---------+------+-------------------------------------+
If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:
ProxyPass "/node1" "http://100.120.110.43:8080/"
ProxyPassReverse "/node1" "http://100.120.110.43:8080/"
ProxyPass "/node2" "http://100.120.110.43:8081/"
ProxyPassReverse "/node2" "http://100.120.110.43:8081/"
The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
ProxyRequests off
ProxyPass /node1 http://0.0.0.0:8080/
ProxyPassReverse /node1 http://0.0.0.0:8080/
ProxyPass /node2 http://0.0.0.0:8081/
ProxyPassReverse /node2 http://0.0.0.0:8081/
</VirtualHost>
You could also point to different hosts/ips that only you have access to.
Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend1.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend2.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
</Location>
</VirtualHost>
For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.
These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, secondServerNamein my httpd.conf. Thank you very much!
– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
add a comment |
Just change your node.js server port something like:
var server = app.listen(8080, function() {
console.log('Ready on port %d', server.address().port);
});
where 8080 is node.js server' new port.
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
add a comment |
I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.
I only noticed something here (I kept a note for you in the comment)
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})

When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.
error
doStuff(body);
^
ReferenceError: doStuff is not defined
Ensure that you're doing the same thing and let us know if your issue it resolved.
doStuff(body);was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.
– Sv443
Nov 29 '18 at 9:04
add a comment |
Seem something already running on your 8080 port. Simply change to another port. For example 7000
And make sure that all request you call to nodejs app like this
localhost:7000 // Normal we run at port 80 url simply localhost without port
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
add a comment |
If I want use Apache and Nodejs in same port:
npm http-proxy-middleware
1. Set Apache Port = 81
[apache dir]/conf/httpd.conf
~59: Listen 81
2. Set nodejs APP port = 3050
server.listen(3050);
// on linux ports<1000 require root privilegue
3. Use third proxy APP (http-proxy-middleware)
// https://www.npmjs.com/package/http-proxy-middleware
var express = require('express');
var proxy = require('http-proxy-middleware');
// proxy middleware options
var options = {
target: 'http://localhost:81', // target host ROOT
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
},
router: {
// Examples:
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
// 'dev.localhost:3000' : 'http://localhost:8000',
// 'localhost:9000': 'localhost:9002/sub',
// '/sub': 'http://localhost:9002',
'localhost': 'http://localhost:81', //Root to Apache
'sub.localhost': 'http://localhost:3050', // internal
'sub.mydomain.com': 'http://localhost:3050', //external
},
};
// create the proxy (without context)
// var proxy_wp = proxy(options_wp);
var proxy_node = proxy(options);
// mount `exampleProxy` in web server
var app = express();
app.use(proxy_node);
app.listen(80);
Then:
- localhost:80 - is apache site
- sub.localhost:80 - node
- localhost:80/sub - node, if you set
for task runner i use npm PM2
add a comment |
This is same scenario as using NodeJs in a Shared Hosting. I have written a blogpost about it here
Let me give an excerpt.
Just run the NodeJS server at let's say
8080port.
Now, let's say your Apache serves at http://example.com, create a folder in your
public_htmlorwww. let's say the name isserver. So, your new folder path is http://example.com/server
- create a
.htaccessfile in the server folder
add the following lines,
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8080/ [P,L] #which is your node server ip:port
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L] #same ip:port
This will redirect all the requests from http://example.com/server to your Node server.
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like#which is..and#same ip. please remove that and try. Just tested it in my local setup
– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52926017%2fnodejs-http-listen-on-other-port-than-80%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:
I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.
This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:
I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:
+---------+------+-------------------------------------+
| Backend | Port | Web Address |
+=========+======+=====================================+
| Apache | 80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1 | 8080 | 100.120.110.43:8080 |
| Node2 | 8081 | 100.120.110.43:8081 |
+---------+------+-------------------------------------+
If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:
ProxyPass "/node1" "http://100.120.110.43:8080/"
ProxyPassReverse "/node1" "http://100.120.110.43:8080/"
ProxyPass "/node2" "http://100.120.110.43:8081/"
ProxyPassReverse "/node2" "http://100.120.110.43:8081/"
The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
ProxyRequests off
ProxyPass /node1 http://0.0.0.0:8080/
ProxyPassReverse /node1 http://0.0.0.0:8080/
ProxyPass /node2 http://0.0.0.0:8081/
ProxyPassReverse /node2 http://0.0.0.0:8081/
</VirtualHost>
You could also point to different hosts/ips that only you have access to.
Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend1.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend2.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
</Location>
</VirtualHost>
For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.
These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, secondServerNamein my httpd.conf. Thank you very much!
– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
add a comment |
Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:
I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.
This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:
I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:
+---------+------+-------------------------------------+
| Backend | Port | Web Address |
+=========+======+=====================================+
| Apache | 80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1 | 8080 | 100.120.110.43:8080 |
| Node2 | 8081 | 100.120.110.43:8081 |
+---------+------+-------------------------------------+
If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:
ProxyPass "/node1" "http://100.120.110.43:8080/"
ProxyPassReverse "/node1" "http://100.120.110.43:8080/"
ProxyPass "/node2" "http://100.120.110.43:8081/"
ProxyPassReverse "/node2" "http://100.120.110.43:8081/"
The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
ProxyRequests off
ProxyPass /node1 http://0.0.0.0:8080/
ProxyPassReverse /node1 http://0.0.0.0:8080/
ProxyPass /node2 http://0.0.0.0:8081/
ProxyPassReverse /node2 http://0.0.0.0:8081/
</VirtualHost>
You could also point to different hosts/ips that only you have access to.
Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend1.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend2.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
</Location>
</VirtualHost>
For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.
These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, secondServerNamein my httpd.conf. Thank you very much!
– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
add a comment |
Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:
I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.
This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:
I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:
+---------+------+-------------------------------------+
| Backend | Port | Web Address |
+=========+======+=====================================+
| Apache | 80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1 | 8080 | 100.120.110.43:8080 |
| Node2 | 8081 | 100.120.110.43:8081 |
+---------+------+-------------------------------------+
If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:
ProxyPass "/node1" "http://100.120.110.43:8080/"
ProxyPassReverse "/node1" "http://100.120.110.43:8080/"
ProxyPass "/node2" "http://100.120.110.43:8081/"
ProxyPassReverse "/node2" "http://100.120.110.43:8081/"
The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
ProxyRequests off
ProxyPass /node1 http://0.0.0.0:8080/
ProxyPassReverse /node1 http://0.0.0.0:8080/
ProxyPass /node2 http://0.0.0.0:8081/
ProxyPassReverse /node2 http://0.0.0.0:8081/
</VirtualHost>
You could also point to different hosts/ips that only you have access to.
Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend1.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend2.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
</Location>
</VirtualHost>
For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.
These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.
Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:
I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.
This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:
I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:
+---------+------+-------------------------------------+
| Backend | Port | Web Address |
+=========+======+=====================================+
| Apache | 80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1 | 8080 | 100.120.110.43:8080 |
| Node2 | 8081 | 100.120.110.43:8081 |
+---------+------+-------------------------------------+
If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:
ProxyPass "/node1" "http://100.120.110.43:8080/"
ProxyPassReverse "/node1" "http://100.120.110.43:8080/"
ProxyPass "/node2" "http://100.120.110.43:8081/"
ProxyPassReverse "/node2" "http://100.120.110.43:8081/"
The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
ProxyRequests off
ProxyPass /node1 http://0.0.0.0:8080/
ProxyPassReverse /node1 http://0.0.0.0:8080/
ProxyPass /node2 http://0.0.0.0:8081/
ProxyPassReverse /node2 http://0.0.0.0:8081/
</VirtualHost>
You could also point to different hosts/ips that only you have access to.
Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.
Listen 80
<VirtualHost *:80>
DocumentRoot /www/apache
ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend1.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName www.nodebackend2.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
</Location>
</VirtualHost>
For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.
These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.
answered Nov 23 '18 at 18:45
willascendwillascend
743310
743310
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, secondServerNamein my httpd.conf. Thank you very much!
– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
add a comment |
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, secondServerNamein my httpd.conf. Thank you very much!
– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
Thanks for your answer! I am currently at work and can't test it out, but I'll make sure to try it out later today.
– Sv443
Nov 26 '18 at 10:27
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
I tried to do exactly what you said and copied the first bit of code to the httpd.conf (I removed the Listen 80 because I already have it). Also I uncommented both mod_proxy and mod_proxy_http. But restarting the server resulted in Apache not wanting to start up. This is my httpd.conf: pastebin.com/NHDzsLeV
– Sv443
Nov 29 '18 at 9:18
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
It was not starting up because I didn't put the DocumentRoot in quotes. Now it gives me a 404 Error on POST request which wouldn't be a huge problem but now still Apache takes the request and doesn't redirect it to 0.0.0.0:8081
– Sv443
Nov 29 '18 at 9:24
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, second
ServerName in my httpd.conf. Thank you very much!– Sv443
Nov 29 '18 at 9:51
After a bit of experimentation, I finally got it to work with the first code snippet you provided. The problem was that I misconfigured my request and that I had a stray, second
ServerName in my httpd.conf. Thank you very much!– Sv443
Nov 29 '18 at 9:51
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
Awesome! Glad you got it working!
– willascend
Nov 30 '18 at 16:50
add a comment |
Just change your node.js server port something like:
var server = app.listen(8080, function() {
console.log('Ready on port %d', server.address().port);
});
where 8080 is node.js server' new port.
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
add a comment |
Just change your node.js server port something like:
var server = app.listen(8080, function() {
console.log('Ready on port %d', server.address().port);
});
where 8080 is node.js server' new port.
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
add a comment |
Just change your node.js server port something like:
var server = app.listen(8080, function() {
console.log('Ready on port %d', server.address().port);
});
where 8080 is node.js server' new port.
Just change your node.js server port something like:
var server = app.listen(8080, function() {
console.log('Ready on port %d', server.address().port);
});
where 8080 is node.js server' new port.
answered Oct 22 '18 at 9:40
Hendro FebrianHendro Febrian
627
627
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
add a comment |
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
That doesn't work, even if I open that port on my router
– Sv443
Oct 22 '18 at 10:28
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
Can you show me how you set up your node.js server? Embed your code to your question
– Hendro Febrian
Oct 22 '18 at 10:51
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
I added the snippet. I can't post everything as it is used for my work though.
– Sv443
Oct 22 '18 at 11:53
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
change port to 8000
– Hendro Febrian
Oct 22 '18 at 11:54
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
Still doesn't work.
– Sv443
Oct 23 '18 at 12:20
add a comment |
I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.
I only noticed something here (I kept a note for you in the comment)
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})

When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.
error
doStuff(body);
^
ReferenceError: doStuff is not defined
Ensure that you're doing the same thing and let us know if your issue it resolved.
doStuff(body);was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.
– Sv443
Nov 29 '18 at 9:04
add a comment |
I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.
I only noticed something here (I kept a note for you in the comment)
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})

When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.
error
doStuff(body);
^
ReferenceError: doStuff is not defined
Ensure that you're doing the same thing and let us know if your issue it resolved.
doStuff(body);was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.
– Sv443
Nov 29 '18 at 9:04
add a comment |
I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.
I only noticed something here (I kept a note for you in the comment)
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})

When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.
error
doStuff(body);
^
ReferenceError: doStuff is not defined
Ensure that you're doing the same thing and let us know if your issue it resolved.
I haven't really understood what you meant by you're not getting any response, because I ran the same code and it works fine for me.
I only noticed something here (I kept a note for you in the comment)
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
doStuff(body); //you haven't defined doStuff so you will end up with a error message on this line, but you sever will still run fine
});
res.writeHead(200, {'Content-Type': 'text'});
res.end('received request successfully');
}
else {
res.writeHead(405, {'Content-Type': 'text'});
res.end('not allowed method ' + req.method + ', try again with GET or POST');
}
})

When running your post request, don't forget to add "" in your body area, select raw then choose JSON(application/json). That should run fine for you, except you might get a reference error as shown below, but you should still get your response of received request successfully.
error
doStuff(body);
^
ReferenceError: doStuff is not defined
Ensure that you're doing the same thing and let us know if your issue it resolved.
answered Nov 27 '18 at 22:01
antzshrekantzshrek
2,99031735
2,99031735
doStuff(body);was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.
– Sv443
Nov 29 '18 at 9:04
add a comment |
doStuff(body);was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.
– Sv443
Nov 29 '18 at 9:04
doStuff(body); was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.– Sv443
Nov 29 '18 at 9:04
doStuff(body); was just a placeholder. In reality I have another function. I never get any request on the server, it doesn't show any kind of activity.– Sv443
Nov 29 '18 at 9:04
add a comment |
Seem something already running on your 8080 port. Simply change to another port. For example 7000
And make sure that all request you call to nodejs app like this
localhost:7000 // Normal we run at port 80 url simply localhost without port
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
add a comment |
Seem something already running on your 8080 port. Simply change to another port. For example 7000
And make sure that all request you call to nodejs app like this
localhost:7000 // Normal we run at port 80 url simply localhost without port
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
add a comment |
Seem something already running on your 8080 port. Simply change to another port. For example 7000
And make sure that all request you call to nodejs app like this
localhost:7000 // Normal we run at port 80 url simply localhost without port
Seem something already running on your 8080 port. Simply change to another port. For example 7000
And make sure that all request you call to nodejs app like this
localhost:7000 // Normal we run at port 80 url simply localhost without port
edited Nov 28 '18 at 9:33
antzshrek
2,99031735
2,99031735
answered Nov 28 '18 at 4:43
Truong DangTruong Dang
687412
687412
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
add a comment |
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
Nothing is running on that port. I have already checked with netstat. Also I tried 8081.
– Sv443
Nov 29 '18 at 9:02
add a comment |
If I want use Apache and Nodejs in same port:
npm http-proxy-middleware
1. Set Apache Port = 81
[apache dir]/conf/httpd.conf
~59: Listen 81
2. Set nodejs APP port = 3050
server.listen(3050);
// on linux ports<1000 require root privilegue
3. Use third proxy APP (http-proxy-middleware)
// https://www.npmjs.com/package/http-proxy-middleware
var express = require('express');
var proxy = require('http-proxy-middleware');
// proxy middleware options
var options = {
target: 'http://localhost:81', // target host ROOT
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
},
router: {
// Examples:
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
// 'dev.localhost:3000' : 'http://localhost:8000',
// 'localhost:9000': 'localhost:9002/sub',
// '/sub': 'http://localhost:9002',
'localhost': 'http://localhost:81', //Root to Apache
'sub.localhost': 'http://localhost:3050', // internal
'sub.mydomain.com': 'http://localhost:3050', //external
},
};
// create the proxy (without context)
// var proxy_wp = proxy(options_wp);
var proxy_node = proxy(options);
// mount `exampleProxy` in web server
var app = express();
app.use(proxy_node);
app.listen(80);
Then:
- localhost:80 - is apache site
- sub.localhost:80 - node
- localhost:80/sub - node, if you set
for task runner i use npm PM2
add a comment |
If I want use Apache and Nodejs in same port:
npm http-proxy-middleware
1. Set Apache Port = 81
[apache dir]/conf/httpd.conf
~59: Listen 81
2. Set nodejs APP port = 3050
server.listen(3050);
// on linux ports<1000 require root privilegue
3. Use third proxy APP (http-proxy-middleware)
// https://www.npmjs.com/package/http-proxy-middleware
var express = require('express');
var proxy = require('http-proxy-middleware');
// proxy middleware options
var options = {
target: 'http://localhost:81', // target host ROOT
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
},
router: {
// Examples:
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
// 'dev.localhost:3000' : 'http://localhost:8000',
// 'localhost:9000': 'localhost:9002/sub',
// '/sub': 'http://localhost:9002',
'localhost': 'http://localhost:81', //Root to Apache
'sub.localhost': 'http://localhost:3050', // internal
'sub.mydomain.com': 'http://localhost:3050', //external
},
};
// create the proxy (without context)
// var proxy_wp = proxy(options_wp);
var proxy_node = proxy(options);
// mount `exampleProxy` in web server
var app = express();
app.use(proxy_node);
app.listen(80);
Then:
- localhost:80 - is apache site
- sub.localhost:80 - node
- localhost:80/sub - node, if you set
for task runner i use npm PM2
add a comment |
If I want use Apache and Nodejs in same port:
npm http-proxy-middleware
1. Set Apache Port = 81
[apache dir]/conf/httpd.conf
~59: Listen 81
2. Set nodejs APP port = 3050
server.listen(3050);
// on linux ports<1000 require root privilegue
3. Use third proxy APP (http-proxy-middleware)
// https://www.npmjs.com/package/http-proxy-middleware
var express = require('express');
var proxy = require('http-proxy-middleware');
// proxy middleware options
var options = {
target: 'http://localhost:81', // target host ROOT
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
},
router: {
// Examples:
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
// 'dev.localhost:3000' : 'http://localhost:8000',
// 'localhost:9000': 'localhost:9002/sub',
// '/sub': 'http://localhost:9002',
'localhost': 'http://localhost:81', //Root to Apache
'sub.localhost': 'http://localhost:3050', // internal
'sub.mydomain.com': 'http://localhost:3050', //external
},
};
// create the proxy (without context)
// var proxy_wp = proxy(options_wp);
var proxy_node = proxy(options);
// mount `exampleProxy` in web server
var app = express();
app.use(proxy_node);
app.listen(80);
Then:
- localhost:80 - is apache site
- sub.localhost:80 - node
- localhost:80/sub - node, if you set
for task runner i use npm PM2
If I want use Apache and Nodejs in same port:
npm http-proxy-middleware
1. Set Apache Port = 81
[apache dir]/conf/httpd.conf
~59: Listen 81
2. Set nodejs APP port = 3050
server.listen(3050);
// on linux ports<1000 require root privilegue
3. Use third proxy APP (http-proxy-middleware)
// https://www.npmjs.com/package/http-proxy-middleware
var express = require('express');
var proxy = require('http-proxy-middleware');
// proxy middleware options
var options = {
target: 'http://localhost:81', // target host ROOT
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
},
router: {
// Examples:
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
// 'dev.localhost:3000' : 'http://localhost:8000',
// 'localhost:9000': 'localhost:9002/sub',
// '/sub': 'http://localhost:9002',
'localhost': 'http://localhost:81', //Root to Apache
'sub.localhost': 'http://localhost:3050', // internal
'sub.mydomain.com': 'http://localhost:3050', //external
},
};
// create the proxy (without context)
// var proxy_wp = proxy(options_wp);
var proxy_node = proxy(options);
// mount `exampleProxy` in web server
var app = express();
app.use(proxy_node);
app.listen(80);
Then:
- localhost:80 - is apache site
- sub.localhost:80 - node
- localhost:80/sub - node, if you set
for task runner i use npm PM2
answered Nov 28 '18 at 23:30
mdimai666mdimai666
1914
1914
add a comment |
add a comment |
This is same scenario as using NodeJs in a Shared Hosting. I have written a blogpost about it here
Let me give an excerpt.
Just run the NodeJS server at let's say
8080port.
Now, let's say your Apache serves at http://example.com, create a folder in your
public_htmlorwww. let's say the name isserver. So, your new folder path is http://example.com/server
- create a
.htaccessfile in the server folder
add the following lines,
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8080/ [P,L] #which is your node server ip:port
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L] #same ip:port
This will redirect all the requests from http://example.com/server to your Node server.
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like#which is..and#same ip. please remove that and try. Just tested it in my local setup
– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
|
show 1 more comment
This is same scenario as using NodeJs in a Shared Hosting. I have written a blogpost about it here
Let me give an excerpt.
Just run the NodeJS server at let's say
8080port.
Now, let's say your Apache serves at http://example.com, create a folder in your
public_htmlorwww. let's say the name isserver. So, your new folder path is http://example.com/server
- create a
.htaccessfile in the server folder
add the following lines,
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8080/ [P,L] #which is your node server ip:port
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L] #same ip:port
This will redirect all the requests from http://example.com/server to your Node server.
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like#which is..and#same ip. please remove that and try. Just tested it in my local setup
– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
|
show 1 more comment
This is same scenario as using NodeJs in a Shared Hosting. I have written a blogpost about it here
Let me give an excerpt.
Just run the NodeJS server at let's say
8080port.
Now, let's say your Apache serves at http://example.com, create a folder in your
public_htmlorwww. let's say the name isserver. So, your new folder path is http://example.com/server
- create a
.htaccessfile in the server folder
add the following lines,
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8080/ [P,L] #which is your node server ip:port
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L] #same ip:port
This will redirect all the requests from http://example.com/server to your Node server.
This is same scenario as using NodeJs in a Shared Hosting. I have written a blogpost about it here
Let me give an excerpt.
Just run the NodeJS server at let's say
8080port.
Now, let's say your Apache serves at http://example.com, create a folder in your
public_htmlorwww. let's say the name isserver. So, your new folder path is http://example.com/server
- create a
.htaccessfile in the server folder
add the following lines,
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8080/ [P,L] #which is your node server ip:port
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L] #same ip:port
This will redirect all the requests from http://example.com/server to your Node server.
edited Nov 29 '18 at 9:31
answered Nov 28 '18 at 18:49
Aritra ChakrabortyAritra Chakraborty
2,45811015
2,45811015
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like#which is..and#same ip. please remove that and try. Just tested it in my local setup
– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
|
show 1 more comment
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like#which is..and#same ip. please remove that and try. Just tested it in my local setup
– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
This gives me a 500 Internal Server Error.
– Sv443
Nov 29 '18 at 9:02
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
can you check if request actually coming to the nodejs server or not? because i have same settings working for me just fine.
– Aritra Chakraborty
Nov 29 '18 at 9:08
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
No, it isn't. I just get a 500 Error.
– Sv443
Nov 29 '18 at 9:25
did you add the comments as well? like
#which is.. and #same ip . please remove that and try. Just tested it in my local setup– Aritra Chakraborty
Nov 29 '18 at 9:31
did you add the comments as well? like
#which is.. and #same ip . please remove that and try. Just tested it in my local setup– Aritra Chakraborty
Nov 29 '18 at 9:31
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
That doesn't change a thing sadly.
– Sv443
Nov 29 '18 at 9:37
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52926017%2fnodejs-http-listen-on-other-port-than-80%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to set reverse proxy for that. Google that out
– Ankit Agarwal
Oct 22 '18 at 9:21
@AnkitAgarwal thanks, I'll look it up
– Sv443
Oct 22 '18 at 9:25
What's the error you get when you change the port?
– willascend
Nov 23 '18 at 17:16
@willascend sorry for the late reply. I get no error. It starts normally. It just can't get any data.
– Sv443
Nov 27 '18 at 14:10
1
Got it. That indicates that the server is likely running on the port as specified. It does sound like the issue is accessing the node servers without using the port in the url. Please try the reverse proxy to get this to work. It may also help to provide additional information on how you are setting up your apache server and how you expect to access the node servers externally.
– willascend
Nov 27 '18 at 17:43