There is an existing node.js application implemented using geddy framework, it is started by Heroku's foreman like so:
web: geddy
I am working on making it into a Heroku add-on. Heroku has a way to auto-generate the skeleton code necessary for an add-on, but it is implemented using express. It is started by this command:
web: node web.js
Internally, Heroku only allocates 1 port per app (that gets external traffic routed to it). Is there a way to start both existing geddy app and add-on express app on the same port? Or have some type of an application level router that would forward to geddy or express based on incoming request path?
Assuming you are on Heroku and are limited to only Node.js apps, I would suggest you to start a new node instant as a reverse proxy. A quick and dirty example would be the following:
proxy.js
var http = require('http'),
httpProxy = require('http-proxy');
var options = {
pathnameOnly: true,
router: {
'/foo': '127.0.0.1:8001',
'/bar': '127.0.0.1:8002'
}
};
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(8000);
first.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('I am the first server!\n');
}).listen(8001);
second.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('I am the second server!\n');
}).listen(8002);
start all three scripts using node, and the test result is as follows:
cloud#wishlist:~$ curl localhost:8000/foo
I am the first server!
cloud#wishlist:~$ curl localhost:8000/bar
I am the second server!
which is exactly what you need: something that makes it look like two apps are listening on the same port. For more detail, look into the node http-proxy module
Related
I am trying to run the google assistant example webhook nodejs application(index.js) on my own server, but don't know what is the webhosting setup for this..
https://codelabs.developers.google.com/codelabs/your-first-action-on-google-with-webhook/#2
What is the environment to run this app on my server? Since it's not running as a listening server, I can't use nginx, node_cgi is not mature with apache, how am I supposed to run this sample?
Excellent point, and you should be sure to file a bug request on the page to indicate it is unclear.
The code, as presented, is meant to run using Google Cloud Functions.
This doesn't mean you can't run it on your own server - just that you need to know how to run a Node.js server outside of your Apache or Nginx environment. I've seen a number of configurations, but typically you'll have the Node.js server application running and listening to a local port and have a proxy between your externally facing web server at a particular path and this port.
But even that isn't sufficient in this case - the code itself doesn't listen on a port - it expects to be handed a request and response object in the form that Express.js with a JSON middleware can handle. To do that, you'll need to have installed the Express.js library and then start listening with code such as:
const express = require('express');
const app = express();
app.use( express.json() );
app.get('/', (req, res) => exports.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Thanks for the help to #Prisoner and #Ido Green link works even better! The minimum to run the sample I did the following:
Create a new nodejs project with mainfile main.js, install express and actions-on-google
mkdir googleActionServer
cd googleActionServer
npm init
npm install --save actions-on-google
npm install --save express`
Copy the index.js from google and put this into main.js
const express = require('express');
const bitcoinInfo = require("./index");
const app = express();
app.use( express.json() );
app.post('/', (req, res) => bitcoinInfo.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Start the application by running:
node ./main.js
To test with DialogFlow, download and install ngrok to /usr/local/bin for ex and then run:
ngrok http 3000
Ngrok will give you an url that is accesible from outside and forward the requests to the nodejs app. It will also create a https for you, so copy paste the https address into DialogFlow webhook address and you are set to go
I am using the npm package http-proxy that claims to help with that, however I am totally incapable to make it work. So far I have only success by making an transparent HTTP proxy server, however when it goes to create a transparent HTTPS proxy server then nothing happens.
I am using an Android device configured to use a proxy with the port where the proxy is expecting to be configured, but nothing is triggered in the nodejs side. Only if I have configured an HTTP proxy server then things seems to be working.
This is the code I have for the HTTPS:
var https = require('https');
var fs = require('fs');
var httpProxy = require('http-proxy');
var options = {
key: fs.readFileSync('./client-key.pem', 'utf8'),
cert: fs.readFileSync('./client-cert.pem', 'utf8')
};
var proxy = httpProxy.createProxyServer({
ssl: options
});
https.createServer(options, function (req, res) {
console.log("new", req.url);
proxy.web(req, res, {
target: req.url
});
}).listen(8000);
If I use the createServer from the http package then it works for http calls (as in that the callback is being fired), however it does not for https with these instruction. Anybody knows what am I doing wrong?
PS: I do not care if I have to use a different npm package.
you can use https://www.npmjs.com/package/transparent-proxy
It is built extending native net.createServer. It acts like a REAL transparent http proxy and It allows you, for example, to easy upstream requests to other proxies and make some requests-chaining through multiple proxies... etc...
Install
npm i transparent-proxy
Use
const ProxyServer = require('transparent-proxy');
//init ProxyServer
const server = new ProxyServer();
//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
console.log('TCP-Proxy-Server started!', server.address());
});
It works on Termux too :)
I'm learning Node.js and I've installed VirtualBox and there ubuntu server 14.04. Node.js is installed as well on ubuntu. I'm doing an exercise in which I created a server which is accessible from guest_localhost:3000 (in ubuntu). The thing is I've not installed any GUI (and I wish to continue in that way). I want to test that the server I created using Node.js is running. To do that I just need to go to server_localhost:3000.
Apparently everything is ok (for example ping host to guest ip), but for some reason it´s not working.
ping ok __ not working
`var http = require('http');
var handleRequest = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to Node Essential Training\n');
};
var server = http.createServer(handleRequest);
server.listen(3000, 'localhost');`
I'll appreciate any help.
Thanks in advance guys.
If you do not have any GUI to visibly verify your page layout, you should be able to access it from your windows machine if they are in the same local network by simply accessing the url http://your_ubuntu_local_ip:3000 where your_ubuntu_local_ip is the local ip your ubuntu machine has internally in your local network. You can find this ip by typing ifconfig in your ubuntu terminal and looking for the ip the network adapter you are using has.
It's solved. The problem was in the code to create the server after all. As I'm learning JavaScript and Node.js I'm watching a tutorial in Lynda.com. The code which doesn't work comes from Lynda. The other from this url: http://blog.modulus.io/build-your-first-http-server-in-nodejs
// THIS CODE DOES NOT WORK
//var http = require('http');
//
//var handleRequest = function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Welcome to Node Essential Training\n');
//};
//
//var server = http.createServer(handleRequest);
//
//server.listen(8080, 'localhost');
//THIS CODE WORKS
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=3000;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
How can I access my server through the browser once I've set up the server ?
In PHP I simply have to put my files in the www/ folder and then go to http://127.0.0.1/index.php, but how can I do that with NodeJS ?
My server's code is from a tutorial :
var app = require(‘express’).createServer();
app.get(‘/’, function(request, response){
response.send(‘Hello Web Designer’);
});
app.listen(8888);
But whenever I go to http://127.0.0.1:8888/ i get a "Problem loading the page". So either my server is not running correctly (hard to tell when the NodeJS console shows "...") or I am not accessing it correctly.
What can I do please ?
you have to send a http head and end the response.
Try this code.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello world');
res.end();
}).listen(8888);
(Pseudo Code)
Also don't use any frameworks to learn how node works.
You can run you index.js now and call localhost:8888
Did you start the server? The apache server runs php, in the nodejs world, there is a javascript runtime that needs to be started.
Try node server.js from the command line (or whatever your file is called) to get it to listen and serve incoming requests.
I'm trying to get my first production node website up (just a basic hello world on my production web server).
The below is what I'm using (basic http proxy to pass off apache websites to port :9000 and node websites to port :8000). I know this part works because apache vhosts are forwarded as I expect. What does not work however is the node part -instead I get the error below
"Cannot GET /"
This is running node 0.8.1 on Ubuntu 12.04
I'm hosting this with forever.js (forever start foo.js). when I echo the NODE_ENV -it shows "production"
It might also be noted that I don't have node_modules on the path (as you will see in my require statements) **not sure if this has anything to do with my issue
var httpProxy = require('/usr/local/lib/node_modules/http-proxy/lib/node-http-proxy');
var express = require('/usr/local/lib/node_modules/express/lib/express');
httpProxy.createServer(function (req, res, proxy) {
var nodeVhosts = ['www.mysite.com'];
var host = req.headers['host'];
var port = nodeVhosts.indexOf(host) > -1
? 8000
: 9000;
proxy.proxyRequest(req, res, {host: 'localhost', port: port});
}).listen(80);
var one = express.createServer();
one.get('/', function(req, res){
res.send('Hello from app one!')
});
var app = express.createServer();
app.use(express.vhost('localhost', one));
app.listen(8000);
Since you are running Ubuntu, you might take a look at upstart. In case you don't know, upstart replaces the old-school-unix init-scripts approach to starting and stopping services. (Those were dark, scary days!) If you want your app to start automatically when your box boots/reboots and restart automatically after it (your app) crashes, then you want upstart. Learning the basics of upstart is easy, and then you have a tool that you can use over and over again, whether it's node, apache, nginx, postfix, mongodb, mysql, whatever!
I mean no disrespect to the good folks who work on the forever module. Arguably, it does have a solid use case, but too often it is used to imperfectly duplicate the bedrock which is already on your system -- upstart. Also, you might Google for some of the comments made by experienced users and some node.js committers about the forkability of node.js and the pitfalls, which are very relevant to forever.
I'd like to post links, but I don't have enough rep yet. Hopefully what I wrote is enough to google on.
Good luck!
http-proxy module doesn't change the host header of the request, and that's what connect/express vhost uses to distinguish virtualhosts.
In this line:
proxy.proxyRequest(req, res, {host: 'localhost', port: port});
you tell the proxy server to proxy the unchanged request to localhost:port.
So what you need to do is to change:
var app = express.createServer();
app.use(express.vhost('localhost', one));
app.listen(8000);
to:
var app = express.createServer();
app.use(express.vhost('www.mysite.com', one));
app.listen(8000);
and it should work.
Alternatively you can simply set the req.headers.host to localhost before proxying.