简体   繁体   中英

How to get node web client to talk to REST API on Heroku?

I have a node web client called bidsell, and a small Python Tornado REST API called quote. Bidsell when triggered makes regular http get calls to quote. Quote duely returns random price information as json. Works locally - want to share it online, but how? Heroku looks promising. Have tried already to deploy both bidsell and quote in the same project on heroku, each running within their own heroku web dyno or deployment container. From the logs "heroku log" both are installed correctly but only one appears to be running. So I can access the front page url of bidsell, for example, but when bidsell is triggered to go fetch quote info the quote service is not found :-( Should I be using another deployment pattern?

ok so as jr0cket suggested I created 2 heroku projects - one for the bidsell node project and one for the quote service.

In addition to the bidsell node project source files I had a procfile containing the following:

web: npm start

and a scripts section in package.json informing heroku how to start the app:

 "scripts": {
   "start": "gulp serve"
 }

In addition to the quoteService source python file I had a procfile containing the following:

web: python quoteService.py

and a requirements.txt file containing:

tornado==3.1.1
pyrestful==0.4.1

Had the following proxy.js as middleware in the bidsell app:

 'use strict'; var proxyMiddleware = require('http-proxy-middleware'); var options = { target: 'http://quoteservce.herokuapp.com:80', changeOrigin: true }; var proxy = proxyMiddleware('/quote', options); module.exports = function(){ return [proxy]; } 

being called from server.js:

 'use strict'; .. var middleware = require('./proxy'); module.exports = function(options) { function browserSyncInit(baseDir, browser) { browser = browser === undefined ? 'default' : browser; .. var server = { baseDir: baseDir, routes: routes }; server.middleware = middleware(); browserSync.instance = browserSync.init({ port: (process.env.PORT || 5000), startPath: '/', server: server, browser: browser }); } .. gulp.task('serve', ['watch'], function () { browserSyncInit([options.tmp + '/serve', options.src]); }); .. }; 

to allow communication between bidsell and quoteService. For further background info take a look here

The running app you can find here .

May take a little while for the idle free-tier heroku dynos to fire up ;-)

Bidsell project on git . QuoteService project on git .

As your project is two seperate technology stacks, the easiest approach is to deploy these as two separate Heroku apps. This gives you simple way to create the specific environment (languages, runtimes, libraries) needed for each app / service.

You could create an Heroku configuration variable QUOTE_REST_API for the node web client that points to the external web address. For example, using the heroku toolbelt

heroku config:set QUOTE_REST_API=https://quote-api.herokuapp.com/

Using the QUOTE_REST_API configuration variable in your node client would give a simple way to change the address of the quote without having to change your code.

If you are running two separate projects in one Heroku app, you need to ensure that you have two web: entries for the Procfile to start your separate processes. Only processes marked as web will listen to web traffic.

You may not be able to run two different web processes if you use the free tier of heroku.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM