Deploy FeathersJS App on Heroku - node.js

I'm trying to deploy my feathersjs web app on heroku, and since feathers is simply an express wrapper I thought it was like deploy an ordinary node app. I got the "npm start" script on my package.json, I added heroku remote to my git repo and when I push heroku run "yarn install" and the "npm start" script. But just when the app start, an error occurs:
heroku logs
I can't figure out what happen, any suggestions?
Maybe I could dockerize my app, someone could help me to find the proper implementation?
Thanks everybody

It is the same as Express but the generated application will by default use feathers-configuration to pull in your application settings. From the error message it looks like you are not providing a proper NODE_ENV environment variable which has to be set to production when deploying to Heroku.

Working port of feathers-chat app to Heroku PostgreSQL via Sequelize
I got a port of the hello world https://github.com/feathersjs/feathers-chat running on Heroku right now!
source: https://github.com/cirosantilli/feathers-chat/tree/sequelize-pg
live until it breaks: https://cirosantilli-feathersjs-chat.herokuapp.com/
Key source changes
Set 'postgres' to the DATABASE_URL environment variable config/production.json:
+ "postgres": "DATABASE_URL"
Heroku also exports PORT which was also already in that file before my patch.
Pass dialiectOptions to the DB connection as per: Can't connect to heroku postgresql database from local node app with sequelize
+ const sequelize = new Sequelize(connectionString, {
+ dialect: 'postgres',
+ logging: false,
+ define: {
+ freezeTableName: true
+ },
+ dialectOptions: {
+ // https://stackoverflow.com/questions/27687546/cant-connect-to-heroku-postgresql-database-from-local-node-app-with-sequelize
+ // https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js
+ // https://stackoverflow.com/questions/58965011/sequelizeconnectionerror-self-signed-certificate
+ ssl: {
+ require: true,
+ rejectUnauthorized: false
+ }
+ }
+ });
Key Heroku settings
enable the PostgreSQL Heroku add-on with:
heroku addons:create heroku-postgresql:hobby-dev-
This automatically sets the DATABASE_URL environment variable for us.
in config/production.json edit host to your correct value
in the Heroku app Settings, set the NODE_ENV environment variable to production
Bibliography:
https://github.com/feathersjs/feathers/issues/1647

try set "NODE_CONFIG_DIR" to "app/config/"

Related

Hide password by ignoring file in .gitignore, but unable to deploy to Heroku?

I was trying to deploy my Node JS application to Heroku. Heroku was connected to my Github account and deployed through Github. In my Node JS application, I created a SQL pool file awsPool.js using the following code:
const mysql = require('mysql');
const awsPool = mysql.createPool({
connectionLimit: 10,
host: "myDb.abcdefg.eu-west-2.rds.amazonaws.com",
user: "myUsername",
password: "myPassword",
port: 3306,
database: 'myDb',
debug: false
});
module.exports = awsPool;
And imported it in my Express application. The pool contains credentials such as my username and password, so I set them as ignored in .gitignore. However, when trying to deploy the application to Heroku, Heroku gave me this error:
Error: Cannot find module './awsPool'
I understand this is likely due to awsPool.js being not tracked in my Github, but how can I properly hide my credentials and deploy to Heroku?
You can use ponto .env files that will define your credentials. So when you are going to deploy your application on Heroku you will only need to define your environment variables

Sequelize Credentials for Deploying Node.js App to Heroku

I am currently learning Node.js (with Express, Postgres, Sequelize) and have a very simple (API) app running locally (it's working, tried with Postman) I now want to deploy to Heroku. I have some basic Heroku knowledge as I've used Rails in the past, but I am stuck with some things Rails handles behind the scenes.
I've set up a config.js file with some production credentials generated when running npx sequelize-cli init, I've updated my local settings (this works!) but what do I need to add as my production credentials? Do I need to provide them on my own? Or are those provided by Heroku?
Please note — I've already setup my pipeline and app as well as Postgres on Heroku but when trying to connect to my app via the cli, e.g. heroku run bash --app name-of-my-app and running npx sequelize-cli db:create it gives me the following error:
Loaded configuration file "config/config.js".
Using environment "production".
TypeError: Cannot read property 'replace' of undefined
at Object.removeTicks (/app/node_modules/sequelize/lib/utils.js:347:12)
at Object.quoteIdentifier (/app/node_modules/sequelize/lib/dialects/abstract/query-generator/helpers/quote.js:50:35)
at PostgresQueryGenerator.quoteIdentifier (/app/node_modules/sequelize/lib/dialects/abstract/query-generator.js:891:24)
at getCreateDatabaseQuery (/app/node_modules/sequelize-cli/lib/commands/database.js:77:50)
at Object.exports.handler (/app/node_modules/sequelize-cli/lib/commands/database.js:45:17)
Further notes:
I am using Node 14.0.0 and sequelize 6.3.0
I've added "engines": { "node": "14.x" } to my package.json
I don't have a procfile as Heroku states it's not needed anymore
My app entrypoint is app.js
Putting this up here, in case someone else is having similar troubles:
Just figured it out, it's actually pretty straight forward but I was a bit confused by the default config.js setup provided when running npx sequelize-cli init.
this is what my production settings look like:
production: {
use_env_variable: 'DATABASE_URL',
dialect: 'postgres'
}

"Error: Configuration property is not defined" on `heroku local web` - Config / Heroku

I am trying to setup locally an existing Heroku app. To do so, I git cloned the app from our repo in Github. Then I added the git remote for heroku.
Normally, builds to the server happen with Travis CI, but now I want to run the app locally straight from heroku.
heroku config returns the config variables just fine.
But heroku local web returns the error
throw new Error('Configuration property "' + property + '" is not defined');
Error: Configuration property "redis.url" is not defined
redis.js
import config from 'config';
const redis = new Redis(config.get('redis.url'), {
keyPrefix: 'kw:',
});
The config folder is in the root of my repo.
config/default.js
import dotenv from 'dotenv';
dotenv.config({ silent: true });
module.exports = {
...,
redis: {
url: process.env.REDIS_URL || process.env.REDISCLOUD_URL,
},
...
}
Relevant scripts in package.js
"start:server": "./node_modules/.bin/babel-node src/server.js",
"start:worker": "./node_modules/.bin/babel-node src/worker/worker.js",
"start": "./node_modules/.bin/nf start",
If I run npm run start:server I get the same error.
If I comment out the redis.url code I get the same error but for another variable in the config.
What am I missing? Let me know if you require clarification.
I had to create a local .env file.
I got confused because I was reading that you dont need a .env file with Heroku. But this is not the case for local setups it seems.
heroku config -s > .env
will copy those files to the local .env it seems. seems strange that this isn't the default, but so it goes.
Once you do that, run your app with heroku local.
I found the answer here: https://www.debugcn.com/en/article/31153943.html

How to fix "Error: The server does not support SSL connections" when trying to access database in localhost?

I am following Heroku's node.js tutorial to provision a Postgres database.
After creating a simple table and connecting to localhost:5000/db, I get an error saying "Error: The server does not support SSL connections".
I've been searching for solutions for hours but can't seem to fix it. Your help will be greatly appreciated. Thank you!
Here I provide a workaround for the question, and not the title of this post. A better answer to the post overall would probably address configuring SSL for the local machine.
I arrived here trying to resolve the problem of finishing that Heroku tutorial mentioned in the question and setting up the Postgres database to work locally as well as remotely.
const pool = new Pool({
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:<your admin password>#localhost:5432/<your db name>',
ssl: process.env.DATABASE_URL ? true : false
})
My idea is to use SSL on the app I deploy but dodge SSL altogether on the local machine. By simply skipping SSL config on the local machine I am able to concentrate my efforts on developing a working app that still uses Heroku's built in SSL.
I use the Heroku environment variables to detect their environment versus my own and I select values accordingly in the code sample above. For me this has worked both locally and remotely.
This is the new form Heroku is working today in 2021, they made small corrections in the connection.
const pool = (() => {
if (process.env.NODE_ENV !== 'production') {
return new Pool({
connectionString: process.env.DATABASE_URL,
ssl: false
});
} else {
return new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
} })();
They changed a bit, using this way you can keep local and heroku available.
I had the same issue for setting up my local environment after the tutorial. Sticking to the command heroku local to start my server fixed it for me. This command is detailed in the Run the app locally section of the tutorial.
In my case I added a Express server and a client side using React + Webpack. My package.json scripts for local development look like:
"dev": "run-p dev:*",
"dev:server": "heroku local -f Procfile.dev",
"dev:webpack": "webpack --watch --config webpack.dev.js --mode development",
run-p is just from npm-run-all to run all scripts starting with dev at the same time.
The server uses the heroku local command and a specific Procfile for local development (use -f flag to do this), where I start a server locally with nodemon to watch for changes (see the Procfile.dev below).
The webpack script builds and watches for changes on the client side with React.
Procfile.dev for local development:
web: nodemon index.js
Procfile for production:
web: node --optimize_for_size --max_old_space_size=920 --gc_interval=100 index.js
Then the same code for connecting to the DB from the Provision a database section works for me:
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
I came here not looking for a localhost solution (I wanted to connect to my db which is online), however, this turned up as the first google result, so I'll add a solution for not localhost issues here:
Check out the .env file to duplicate the Environment variables you setup in your heroku environment; see here: https://devcenter.heroku.com/articles/heroku-local#set-up-your-local-environment-variables
const pool = new Pool({
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:#localhost:5432/',
ssl: process.env.DATABASE_URL ? true : false
})
In your code, you add this snippet with the credentials and connection string details, here process.env.DATABASE_URL comes from environment file, if it is there as it will enable ssl mode, else in local without ssl it works. Make sure, you will mention env variable only for other than local. So on both environment it works.
This helped me, when I was using connectionUrl.
postgres://password:postgres#localhost:5432/database?sslmode=disable
You might have noticed I have added ?sslmode=disable at the end of
connection url.
For more information about sslmode check this out.
To fix this I had to edit the database.js file.
Path to file:
<path-to-your-strapi-project>/config/database.js
if you find something with ssl that is set to true set it to false. In my case, I used Mysql it looked like this:
ssl: env.bool('DATABASE_SSL', true)
Change it to:
ssl: env.bool('DATABASE_SSL', false)
Best solution that I found that worked only:
const client = new Client({
connectionString: productiondbLink || localdblink,
ssl: process.env.DATABASE_URL ? { rejectUnauthorized: false } : false
});
client.connect();

process.env.VARIABLE is undefined in webpack/docker/heroku deployment

I am having some issues getting my app to read the heroku environment variables. The application itself is a node.js react/redux web application ran with webpack-dev-server, deployed in a Docker container which is hosted on Heroku.
I'm setting the variables in a config.js file, which are to be used through the app:
module.exports = {
baseUri: process.env.BASE_URI || "http://localhost:8000",
baseApiUri: process.env.BASE_API_URI || "http://localhost:8080",
port: process.env.PORT || 8000
}
In my index.tsx file I am trying to make a simple get request to the baseApiUri, but am finding that it is defaulting to localhost even though I have these variables as settings in Heroku.
I am getting these results both:
locally when I run
set BASE_URI='http://test.com/'&&set BASE_API_URI='http://testapi.com/'&&set PORT=5000&&webpack-dev-server --host 0.0.0.0
remotely when I run:
webpack-dev-server --host 0.0.0.0

Resources