The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

537960 (7) [Avatar] Offline
#1
Hello Clive,
Really enjoyed coding through the Angular chapters, seemed to put the app on a new level. Thank you.

At the end of chapter 10 I pushed up my app to Heroku and while the app loaded perfectly, there was no data. In the console I see:
'Failed to load resource: net::ERR_CONNECTION_REFUSED localhost:3000/api//locations:1'.


So if I start the app locally with this:
NODE_ENV=production MONGODB_URI=mongodb://<dbuser>:<dbpassword>@ds163402.mlab.com:63402/heroku_bf9tx0rq nodemon

Then both the localhost:3000 and the Heroku app work as designed.

I have confirmed that mongoose is connected to the mLab db with 'Mongoose connected to mongodb://heroku_bf9...' .

Here is a snippet from my db.js file:
const mongoose = require('mongoose');
//DB connection string definition
let dbURI = 'mongodb://localhost/known';
if (process.env.NODE_ENV === 'production') {
dbURI = mongodb://<dbuser>:<dbpassword>@ds163402.mlab.com:63402/heroku_bf9tx0rq

And a snippet from my locations.js file:
const request = require('request');
const apiOptions = {
server : 'http://localhost:3000'
};
if (process.env.NODE_ENV === 'production') {
apiOptions.server = 'https://...herokuapp.com';
}

Any direction on which file (maybe app.js, locations.js or db.js) where I need to make changes would be awesome.

Thank you again.
Dan
Clive.Harber (12) [Avatar] Offline
#2
Hi Dan

Thanks for reading our book and apologies for the delay in responding - I'm knee deep in trying to get the manuscript ready for print.

The issue isn't in the Node part of the application, this particular problem is in the data service in the Angular files.

Specifically in loc8r-data.service.ts. You will need to update the apiBaseUrl to point to the API URL on your Heroku installation and rebuild the application.

If you are looking for a neat solution to having both URL's available and not have to worry about which environment you're deploying to, then I suggest that you use the environments files. Add a new property to environments/environment.ts (something like apiBaseUrl: 'http:localhost:3000/api') and one to environments/environment.prod.ts (apiBaseUrl: <herokuURL>smilie.

In loc8r-data.service.ts import the environments config:

import { environment } from './../environments/environment';


then change
private apiBaseUrl = 'http://localhost:3000/api';


to
private apiBaseUrl = environment.apiBaseUrl;


Angular will use the correct environment based on the build flag. For production build use --prod in the build line

Thanks
537960 (7) [Avatar] Offline
#3
Thank you Clive!
This is great. And please, no worries about the delayed response, I really appreciate your time.

I've got a few more steps in Ch 11 and then I'll re-build and test.

Thank you again.
Dan