Share constant from node.js sever to js files in frontend app - javascript

I have a node.js server and a package.json file. In that file I set some variables. For exemple "version" : 3.0. I can access those variables very easily in the node.js server. (Is there a way to get version from package.json in nodejs code?)
var pjson = require('./package.json');
console.log(pjson.version);`
But how can I pass it to my js app working on the frontend? Can I create a constant.js file that is created went I start the server (only ones).
I do not want to pass the variable as an argument every time I render a page.
I use the ejs to render my pages.
Thank you for your help.

You could set a cookie in the initial response.
response.setHeader("Set-Cookie", ["version=3.0"])

Related

Check Production and Staging environment inside HTML file in a React App

I am trying to integrate a 3rd party app inside my React App. I have to use different keys for production and staging but I am unable to figure out how to achieve that in React and Node applications I can do it by process.env but in index.html I am unable to figure out how can I achieve that.
Here is my pseudocode which I am trying to pull for my index.html file for my React project
<script>
if(env === production){
some_app.init('TOKEN_FOR_PRODUCTION');
}
else{
some_app.init('TOKEN_FOR_STAGING_OR_DEV');
}
</script>
While I personally prefer to store all API keys on the server itself, this guide indicates that there are other ways to do it:
https://www.geeksforgeeks.org/how-to-hide-your-api-keys-from-public-in-reactjs/
Additionally, to define differences between prod and staging in the front end alone, I recommend defining plug in variables using webpack:
https://webpack.js.org/plugins/define-plugin/

How put API key on env files in a Node.js app?

this is my first time im trying to handle " Web api ", so i took this project to get many call but after it running well, when i try to click to "search" it dosent work,
I guess the problem arises from api call because chrome inspector show me that :
I was able to understand on the different forums, for handling apis call with Node.js that must be encapsulated API calls behind "Environment variable".
That the config.js file
When i try to put on the terminal export env.API_KEY='000000000000000' it made me :
export: not valid in this context: env.API_KEY
I hope you can point me in the right direction, I very tried everything, to run it that.
I personally like to use a npm package called dotenv:
You can install it by running npm i dotenv within your api directory.
Have a file called .env within your api directory which contains all of your environment variables:
APP_ID="000000000000000"
API_KEY="000000000000000"
Then change config.js to load all environment variable files when it is executed by including require('dotenv').config():
require('dotenv').config()
module.exports = {
APP_ID: process.env.APP_ID,
API_KEY: process.env.API_KEY,
BASE_URL: 'https://api.adzuna.com/v1/api/jobs',
BASE_PARAMS: 'search/1?&results_per_page=20&content-type=application/json',
};
Note: you will also want to add .env to your .gitingore so that your sensitive API keys aren't included in your git repository

NodeJS - Cant find JSON File

I'm working on a express API and i want to connect to a mysql server with this api. Settings are stored in a settings.json file. I read this using
const config = JSON.parse(require(`fs`).readFileSync('../../settings.json'));
This works if the json file is in the same directory. But in this case, the settings file is in the base directory(./settings.json) but the file from where i want it to access is: ./modules/sql/mysql.js. It doesnt work:
Error: ENOENT: no such file or directory, open '../../settings.json'
Is there a better way to access/read a json file? or what am i doing wrong?
Btw. i dont want to pass the settings as a variable. I already tried it but if possible - i want to avoid it.
File/Directory strcture
did you not miss a ../ there?
try replace '../../settings.json' for '../../../settings.json'
btw, its better to use the __dirname approach, but it should work just fine without that too :)

Cannot write schema.gql to Google's App Engine at runtime NestJS

I am using NestJS and I am using their graphql package.
https://docs.nestjs.com/graphql
Nest Generates the schema.gql file at run time. This is causing an issue with google's App Engine because by default, applications can't write to the file system.
UnhandledPromiseRejectionWarning: Error: EROFS: read-only file system, open 'schema.gql'
I understand how this would be a problem for storing data on the file system, but this schema.gql file that is generated at runtime is nessecary for the app to run and doesn't matter if it gets replicated.
Is there a work around I am missing? Can I get this to generate serverside somehow? Or is there a way I could make it apart of compile time when the application builds?
Any help is appriciated :)
Shout out to the NestJS Discord where this question got answered by #TreeMan360!
Where you have:
autoSchemaFile
in your
GraphqQLModule.forRoot({...
set it to:
true
which gives you an automatic, in memory only, schema. I do this like so:
GraphQLModule.forRoot({
autoSchemaFile: isLocalEnvironment() ? 'api-public.schema.gql' : true,
playground: !isProductionEnvironment(), ...
Following these instructions solved my problem! Niffty huh?

Same config for node.js and for browser javascript

Is it possible to have the same config file for server node.js and client js code?
Now I use .json config for node.js, but can convert it to any format
Without more information, all I can offer is a simple suggestion: Require.js. You can reference a config module in the Node.js code and reference it in the client script. Just make sure that the config file can be served to the client.
// Example reference
var config = require('./data/config.js');

Categories

Resources