Meteor environment variables invalid in production - javascript

I am using Meteor UP for deployment and have set the environment variable both in the mup.json file and a file server/lib/env.js which contains them.
Here is where its being accessed:
Meteor.startup(function() {
// Remove configuration entries in case service is already configured
Accounts.loginServiceConfiguration.remove({
service: "facebook"
});
// Add Facebook configuration entry
Accounts.loginServiceConfiguration.insert({
"service": "facebook",
"appId": process.env.FACEBOOK_1,
"secret": process.env.FACEBOOK_2
});
});
However in the browser I'm getting "Invalid app id: null", but it works in development, any ideas?

Use Meteor.settings.
Development
Define settings in .config/development/settings.json.
Create shell script (dev.sh) in root of your meteor project:
#!/bin/bash
meteor --settings .config/development/settings.json
Instead using command meteor run ./dev.sh
Production (deploy using mup)
mup init creates 'deployment directory' with generated files mup.json and settings.json.
It is important to execute mup init outside of your meteor app directory, so deployment configuration will not be stored on app server.
Usage
Example of settings.json:
{
"service_id":"...",
"service_secret":"...",
"public":{
"service_name":"..."
}
}
If the settings object contains a key named public, then
Meteor.settings.public will be available on the client as well as the
server. All other properties of Meteor.settings are only defined on
the server.
server only:
Meteor.settings.service_id
Meteor.settings.service_secret
server and client :
Meteor.settings.public.service_name
Update:
Changed paths accordingly to Hubert OG's comment

Related

Autodesk Forge web application - from visual studio code to close .exe file

I have a working forge application ( bim360 hub sidebar with forge viewer and some charts).
It is currently running from Visual Studio Code IDE only. I want to build the app into an .exe file in order to be able to send it to a user, upload it to a server with IIS, etc..
General details:
I used Petr Broz tutorial to set up the backend of the viewer and hub
(Forge online training - view your models https://www.youtube.com/watch?v=-O1e3gXCOEQ&t=8986s )
The app is running on Node.js
I tried to use 'nexe' module and build executable file. With this method, I need to specify index.js file ("an entry point") and define a 'nexe.config.js' file. I used the entry point start.js.
Eventually, I managed to create an exe file - and when I run it from the command line, I get an error
Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.
although I have them in the config.js
Main questions:
Is there another way to build a close exe file from visual studio code - for a forge web application?
Am i doing something wrong with the processes I mention above?
Is it even possible to deploy a web application to IIS using an exe file?? all of the documentation points toward Azur, AWS and heroku..
Relevant files:
1) start.js:
const path = require('path');//bringing in built in node js modeules ( to resulve file system path )
const express = require('express');//module to create the express server
const cookieSession = require('cookie-session');
//any piece of code would have an opportunity to handle the request
const PORT = process.env.PORT || 3000;
const config = require('./config.js');
if (config.credentials.client_id == null || config.credentials.client_secret == null) {
console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
return;
}
let app = express();
//static middlewere to check for the front end files (html,js,css)
app.use(express.static(path.join(__dirname, 'public')));//method inside express module: a middlewere for serving static files this line will check in 'public' folder if the request
//that is sent (specific file) is in there. if so - it will ignore the rest of the stack(the rest of the code)
app.use(cookieSession({
// create 2 cookies that stores the name and encripted key
name: 'forge_session',
keys: ['forge_secure_key'],//takes cater of decipher the encription for the forge key for us
maxAge: 14 * 24 * 60 * 60 * 1000 // 14 days, same as refresh token
}));
app.use(express.json({ limit: '50mb' }));//middlewere that looks at the title of the request - and if its .json it will look at the body of the request and parese it to javascript object
app.use('/api/forge', require('./routes/oauth.js'));//adding our custom express routers that will handle the different endpoints.
app.use('/api/forge', require('./routes/datamanagement.js'));
app.use('/api/forge', require('./routes/user.js'));
app.use((err, req, res, next) => {
console.error(err);
res.status(err.statusCode).json(err);
});
app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
2) config.js:
// Autodesk Forge configuration
module.exports = {
// Set environment variables or hard-code here
credentials: {
client_id: process.env.FORGE_CLIENT_ID,
client_secret: process.env.FORGE_CLIENT_SECRET,
callback_url: process.env.FORGE_CALLBACK_URL
},
scopes: {
// Required scopes for the server-side application-->privliges for our internal opperation in the server side ("back end")
internal: ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'],
// Required scope for the client-side viewer-->priveliges for the client ("front end")
public: ['viewables:read']
}
};
Author of the tutorial here :)
I'm not sure how nexe works exactly but please note that the sample app expects input parameters such as FORGE_CLIENT_ID or FORGE_CLIENT_SECRET to be provided as environment variables.
As a first step, try running your *.exe file after setting the env. variables in your command prompt.
If that doesn't work, try hard-coding the input parameters directly into the config.js file (replacing any of the process.env.* references), and then bundle everything into an *.exe file. This is just for debugging purposes, though! You shouldn't share your credentials with anyone, not even inside an *.exe file. So as an alternative I'd suggest that you update the sample app to read the input parameters from somewhere else, perhaps from a local file.
after trying a lot of solutions, i got to the conclusion that the reason that nothing happened was that the oathantication files ( with the clint_id and clint_password) was not embedded in the .exe file.
the way to include those files with the nexe module is to use the flag -r "Foldername/subfoldername/filename.js".
first, crate a nexe.config.js file that would contain the entry point file name to the app. ( in my case, the file name is " start.js")
second, write the following commands in the command line:
cd C:\Projects\MyAppFolder
npm install -g nexe
// specify all the files you want to include inside the exe file
nexe start.js -r "config.js" -r "nexe.config.js" -r "routes/common/oauth.js" -r "routes/*.js" -r "public//." -r ".vscode/**/." -r "package-lock.json" -r "package.json" --build --output "AppName.exe"

Config File in NodeJS using npm config

I have a on-cloud SAAS application built on Angular/NodeJS/Postgres+MongoDB stack that can connect to customer DB, cloud warehouses, S3 buckets etc to load information. Once I get the connection information from Angular front-end I need to store this information for future retrieval .
Example :
Angular Front-end
<form>
Database Type :
Host :
Port :
Username:
Password :
S3Bucket :
Region :
bucket-name :
Access key :
</form>
etc.
I need this information saved for later access. As suggested by Abdullah Deliogullari in the original question, I am trying to use config module npm config. But how do I use package config to write config file and load it in a running application.
ie While my application is running I need to write the (say S3) bucket info to a customer.config file (from frontend JSON) and later when required to retrieve data use the customer.config to connect to S3 bucket.
The "get" portion I am able to understand but the write portion (adding a new section dynamically) is what I am not able to figure out.
Example from frontend when I pass in the values like
["ct_postgres":
{"host":"3.15.xxx.xxx",
"port":"5132",
"dbname":"wcdb"
}]
this should be written to the config file. So something like config.put/write I am looking for. Next time I want to make connection to the customer postgresdb I do config.get() and it provides me the connection details.
Original question
First you should install this package
npm install config
After that create config file into your project directory(name "config" is a must).
In this file create development.json and production.json files(these names are optional). Content of development.json could be like this
{
"SERVER" : {
"port" : 5000
},
"PASSWORDHASH" : {
"saltRounds" : 10
}
}
In your javascript file for example in app.js file, first you should include the library, and then you can get configuration informations via this module
const config = require('config');
const port = config.get("SERVER.port");
app.listen(port);
Last step should be add this development.json file name to NODE_ENV variable
NODE_ENV=development node app.js
So if you want to update your json file based on new data coming from your frontend, you can basically do this
var fs = require('fs');
var file_content = fs.readFileSync("development.json");
var content = JSON.parse(file_content);
content.SERVER.port = 6000;
fs.writeFileSync("development.json", JSON.stringify(content));

how to deploy nodejs api and vuejs app in one server

I have developed node rest api and vuejs web applications,
Im trying to deploy both project in to one aws server which run ubuntu.
Both applications have different port,
domain I try to configure api.example.com for api and example.com for vue app.
I can run both applications once after running the command in SSH, but I need them to run it forever.
What I did,
Deploy to apps separately
Apps can access with ports
I need them access
api.example.com
example.com
what are the step to do,
Any changes host file.
I found another way to deploy vue app and express/nodejs in one server without using PM. This what I did
Build your vue code using npm run build command. This will create a folder dist which should have index.html file and static folder.
Copy dist folder into your server code repository. In my case I created a folder public and moved the dist folder inside public.
In app.js file right before module.exports=app line, copy the following lines of code
//These 2 lines make sure that vue and express app are coming from the same server.
app.use('/static', express.static(path.join(__dirname,"../public/dist/static/")));
app.get('/', function(req,res) {
res.sendFile('index.html', { root: path.join(__dirname, '../public/dist/') });
});
First line make sure that the /static folder is accessible and second line will serve the index.html file when you run the node server. Routing between components will be taken care by vue.
This is how we are running our VueJS UI and ExpressJS REST API from the same server.
We are managing our services with PM2.
VueJS (Dev Environment, You can add the same settings to production)
In package.json add "start": "HOST='0.0.0.0' PORT=80 npm run dev",, where 80 is the port VueJS is listening on, to the "scripts": {..} array. Then, after installing PM2, (for dev) we can start VueJS with cd /location/of/vue/root; sudo pm2 start npm run dev --name Vue -- start. (Make sure that Apache is not running).
Please note that setting the HOST to 0.0.0.0 is important. Do not set it to LocalHost or your Servers IP address or you may run into issues.
ExpressJS
In the /location/of/express/app.js add this similar to the bottom of the file:
app.listen(process.env.PORT || 8081), where 8081 is the port your REST API should be listening on. I can then start it with sudo pm2 start /location/of/express/app.js --name Express
At this point, the VueJS should be available at www.example.com (implied Port 80) and the REST API would be available at www.example.com:8081.
If you want to have api.example.com/ point to the API, you need to make sure that your DNS is pointing the "api" subdomain to the desired port, or you may have to add the port into the URL as above.
Additionally, you can easily follow the logs through PM2 as well with pm2 logs APPNAME --lines 100.

How to host json-server in azure

I am new in the software field, please have patience with my question and mistakes of technical terms:
Premises:-
I have developed an front-end application using Angular4. The baseURL define in angular application is 'http://localhost:3000/'. My application uses restangular api to interact with json-server (I created a folder named json-server and it has db.json and public folder ). It is working perfectly fine when i start the json-server using command:
json-server json-server --watch db.json
My application is finalized and thus I created a production build. Thereafter I moved all my files from dist folder to public folder of json-server. When i start the json-server, my application works fine.
Actual problem:-
Now I wanted to host in azure. I simply copied all file/folder (db.json and public folder) from json-server folder as it is and put them in azure cloud. When azure hosting is done and I open the url in browser I got an error- "you don't have permission to view".
To rectify above error I deleted all files from azure and then I copied all files of dist folder and put them in azure cloud. Using this I could able to see the application in the browser but no images. At image there is an error- Response with status: 0 for URL: null
When I start json-server locally, everything works fine but of course when same web page open from other machines I got the same error- Response with status: 0 for URL: null
Is there any way to run json-server in azure so that all machines/mobile when accessing the url, can see proper web page without any error.
Step to step to run json-server on Azure Web App:
Open your browser and go to App Service Editor (https://<your-app-name>.scm.azurewebsites.net/dev/wwwroot/)
Run the command in the Console (Ctrl+Shift+C)
npm install json-server --save-dev
Put all file/folder (db.json and public folder) into wwwroot folder
Create a server.js with the following content
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen(process.env.PORT, () => {
console.log('JSON Server is running')
})
Click Run (Ctrl+F5), this will generate web.config file automatically and open your website in the browser.
You can use the following to quickly setup a mock service which can serve REST APIs off static JSON files.
json-server
Just install the NodeJS module ($npm install -g json-server). Populate a static json file in the format attached and then run the JSON server ($ json-server --watch db.json --port 3000)

Running Meteor Up how to choose settings file

I am using Meteor Up to deploy my app to production server.
For now I am configuring Meteor.settings through settings-dev.json and settings-prod.json for development and production environments.
I have two main doubts:
How can I run 'mup deploy' command from my machine and choose settings-prod.json?
Is this the best practice to configure my server and resource values using settings-[env].json to deploy my app?
Thanks
Just use the default name /settings.json. It will work. When you execute mup init it will automatically create 2 files.
mup.json - Meteor Up configuration file
settings.json - Settings for Meteor's settings API
And yes it is a good idea to use settings.json. Just be careful to not insert any secret information inside "public": {}
For example:
{
"public": {
"publicKey": 'xxx' // available to the client
},
"oauthSecretKey": "xxx" // available to server only
}

Categories

Resources