Change environment when start sailsJS project - node.js

I have two environment in my sailsjs project: development and production.
In local.js I have this:
module.exports = {
port: process.env.PORT || 1349,
environment: process.env.NODE_ENV || "production"
}
In my development.js I have:
module.exports = {
models: {
connection: "someMongoDb",
migrate: 'alter',
schema : true,
autoPK: true,
autoCreatedAt: true,
autoUpdatedAt: true
},
port: 1348
}
In my production.js I have:
module.exports = {
models: {
connection: "mongoDBPro",
migrate: 'alter',
schema : true,
autoPK: true,
autoCreatedAt: true,
autoUpdatedAt: true
},
port: 1349
}
I would like to select one of this environment when I start my sails project. It is possible when I start with "sails lift" say what environment I choose?

sails lift by default will uses --dev.
You can add --prod to the command to specify a production environment.
Sails will set the environment variable NODE_ENV and load the correct configuration.

Related

Vue 3 and vite cors problems on prod

I have site that was worked on vue 3 and vue-cli with nginx and proxy. So I change from vue-cli to vite v4 and now I get net::ERR_CONNECTION_REFUSED here is my vite.config
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
// "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js",
"#": fileURLToPath(new URL("./src", import.meta.url)),
},
},
base: "/",
build: {
chunkSizeWarningLimit: 3000,
},
server: {
host: true,
cors: false,
proxy: {
"/api": {
target: "http://localhost:3200",
changeOrigin: true,
secure: false,
// ws: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
});
I trying few weeks to fix but no success
I try without cors and try to implement all options for the vite configuration
I finally solved the problem, the wrong set of env variables. In vite when importing the env variables with method import.meta it was not read the NODE_ENV so I need to add environment in the env file so the places where get base URL its read correctly by the environment

laravel 9 + lando = no livereload and no sourceMapping

I just create a new Laravel 9 project and I'm using lando.
I have followed instructions from this : https://sinnbeck.dev/posts/getting-vite-and-laravel-to-work-with-lando
Currently, the site is working, I can update php, css or js code.
But, there is no livereloading and I've got an error in my console about a missing sourcemapping for the css at http://localhost:3009/resources/css/app.css.map
There is a link to the project : https://github.com/CrunchyArtie/warene-next
There is my vite.config.js file:
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
server: {
https: false,
host: true,
port: 3009,
hmr: {host: 'localhost', protocol: 'ws'},
},
});
and my .lando.yml file :
name: warene
recipe: laravel
config:
webroot: ./public
php: '8.1'
xdebug: true
services:
node:
type: node:16
scanner: false
ports:
- 3009:3009
build:
- npm install
tooling:
dev:
service: node
cmd: npm run dev
build:
service: node
cmd: npm run build
EDIT :
With this vite.config.js the livereloading works :
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
server: {
https: false,
host: true,
strictPort: true,
port: 3009,
hmr: {host: 'localhost', protocol: 'ws'},
watch: {
usePolling: true,
}
},
});
With css.devSourcemap: true a sourcemap file is generated and used.
With server.watch.usePolling: true vite will detect file changed inside lando environment.
This is my is my vite.config.js file:
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
css: {
devSourcemap: true,
},
server: {
https: false,
host: true,
strictPort: true,
port: 3009,
hmr: {host: 'localhost', protocol: 'ws'},
watch: {
usePolling: true,
}
},
});

Configuration property "dialect" is not defined

I'm having trouble setting up a project with the config npm package.
My config.js looks like this:
require('dotenv').config();
const { DB_HOST, DB_USERNAME, DB_PASSWORD, SENDGRID_API_KEY } = process.env;
const env = process.env.NODE_ENV;
const config = {
development: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_development",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
test: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_test",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
production: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_production",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
}
};
module.exports = config[env];
In 1 service driver file I have the following line:
const dialect = config.get('dialect');
I get the following error: "Error: Configuration property "dialect" is not defined".
I also tried using 'development.dialect' but that doesn't help either.
Is it possible that the require('config'); doesn't work?
In my Sequelize's index.js file I've got
const config = require(__dirname + '/../config/config.js'); and that seems to work fine.
This github repository provides a good example of using config package.
https://github.com/basarbk/tdd-nodejs
Notes:
for diffrent configs, you must have a file named whatever you use for NODE_ENV. for example for "start": "cross-env NODE_ENV=production node index" create a file called production.js
check config folder in root directory
if you are using window you should use cross-env.

Migration with Sequelize CLI to DigitalOcean Postgres Database Throwing SSL Error

Connecting to my my DigitalOcean database with Sequelize works fine when I'm not migrating. For example, attempting to create a new table works just fine; the code below successfully connects and creates a new table.
sequelize = new Sequelize(config.use_env_variable, config);
sequelize.authenticate().then(console.log('success')).catch((error) => console.log(error));
sequelize.define('test-table', {
test_id: {
type: Sequelize.INTEGER,
},
});
sequelize.sync();
I have a CA certificate .crt file I downloaded from DigitalOcean that I'm passing in with the Sequelize options. My config.js looks like
development: {
use_env_variable: 'postgresql://[digitalocean_host_url]?sslmode=require',
ssl: true,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
ca: fs.readFileSync(`${__dirname}/../.postgresql/root.crt`),
},
},
},
However when I try to create tables using migrations with
npx sequelize-cli db:migrate
I receive the following output and error:
Parsed url postgresql://[digitalocean_host_url]?sslmode=require
ERROR: no pg_hba.conf entry for host [host], user [user], database [database], SSL off
Which is very strange, because SSL is working when I create a table using just Sequelize sync. I have a .sequelizerc file for the sequelize-cli configurations, which looks like this:
const path = require('path');
const env = process.env.NODE_ENV || 'development'
const config = require('./config/config')[env];
module.exports = {
'config': path.resolve('config', 'config.js'),
'url': config.use_env_variable,
'options-path': path.resolve('config', 'sql-options.json')
}
inside my sql-options.json I have the following
{
"use_env_variable": "postgresql://[digitalocean_host_url]?sslmode=require",
"dialect":"postgres",
"ssl": true,
"dialectOptions": {
"ssl": {
"required": true,
"rejectUnauthorized": true,
"ca": "/../.postgresql/root.crt"
}
}
}
I've tried a lot of the advice from various resources, including the sequelize/cli repo. But none of it seems to work. Any advice would be helpful.
I had the same issue and the fix was to add the code below in the migrations config file even though you already have it in the database connection file.
The following code is in the config/config.js file for migrations.
production: {
username: ****,
password: ****,
database: ****,
host: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
This is how my DB connection looks like that was working normally.
const sequelize = new Sequelize({
host: ****,
database: ****,
username: ****,
password: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
});

How to make express.js using pm2 run only one instance on port (8080) and on second instance use another port (8081)

I searched all over, but could not find the answer
for how to config pm2 with Express.js
here is what i have so far based on other's answers and pm2 documentation.
this is on the server main file(index.js):
const port = process.env.NODE_PORT;
I get undefined unless i use || 8080 .
const port = process.env.NODE_PORT || 8080;
I need it working only on dev env for now..
but it seems it does not get what i config on ecosystem.config.js file.
and on my ecosystem.config.js:
module.exports = {
apps: [{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_PORT = 8080 `pm2 start app.js -f`,
NODE_PORT = 8081 `pm2 start app.js -f`,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
PORT: 8081,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}
],
deploy: {
production: {
user: 'node',
host: '212.83.163.1',
ref: 'origin/master',
repo: 'git#github.com:repo.git',
path: '/var/www/production',
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env
production'
}
}
};
I'm using environment variable process.env.NODE_APP_INSTANCE to do this. (https://pm2.io/doc/en/runtime/guide/load-balancing/#cluster-environment-variable)
I'm setting my PORT before starting my server, and then I'm setting server port based on PORT environment variable and NODE_APP_INSTANCE, something like this:
const nodeInstance = parseInt(process.env.NODE_APP_INSTANCE || 0, 10);
const port = process.env.PORT + nodeInstance;

Resources