Pass In Config Values to JS File - javascript

I have a JavaScript file, and I would like to pass in a few config values to my JS file. I originally thought of using an .INI file, but I found out that the browser cannot access these System Config values. Does anyone have any suggestions to alternatives?

I store my configuration in a separate JSON file and load that into the app as a configuration object.
See this answer for examples on how to do this using Require.js or jQuery: requirejs load static JSON file

Related

how to use properties file in vueJs?

hi is there any way to store most usable values and properties in a file like yml - property file and ...
and use them in vueJs components
in java we have spring and it helps us using yml and property file properties in project
is there any thing same in vueJs
In vueJs you can use .env files easily.
In you .env file you can define properties starting with VUE_APP_ and them will be available for you in the whole app.
Ex: .env
VUE_APP_API_URL=http://localhost:5000
To read this property just do it.
process.env.VUE_APP_API_URL
More info here.
you can build an object in a .js file and import that obj as your configuration.
no file system exists in pure front-end
or you can get your configuration from a backend api
You can create a normal JS file and import in the Vue.
Or you might need to read about .env file because between those 2 files you gonna need for constants

How to download the complete project structure from cdnjs using Python

I want to download the complete project from the cdnjs cloud to local folder.
I have tried this:
import requests
files = requests.get("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML%2CSafe.js&ver=4.1")
with open("mathjax.js","w") as file:
file.write(files.text)
Now this download the js file. When I tried using the same code to get the project instead of the js file, the output was weird.
So I tried using the cdnjs and check what happens when I use cdnjs cloud and when I use local file.
I have got this difference as shown in the images:
Using cdnjs:
Using Local file:
How I can get the similar structure as I get when I use cdnjs?
Kindly, advise me.
The URL you are providing to requests module is just the URL of one file MathJax.js, that is why you are getting only that file as output.
What you want is to download the complete directory mathjax/2.7.5/. However, if we request the whole directory, the server forbids such requests.
An alternate approach is to get relative paths of all the files from the main directory, which you already have as you showed in image. You can then download each of the file independently and store it into its respective folder. You'll have the whole directory ready at the end.
Try the following code for this purpose.
import requests
import os
baseUrl="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/" #Base URL for the main directory
#List containing relative paths of all required files
relativePaths=['config/Safe.js?V=2.7.5',
'config/TeX-AMS-MML_HTMLorMML.js?V=2.7.5',
'extensions/Safe.js?V=2.7.5',
'jax/output/SVG/fonts/TeX/fontdata.js?V=2.7.5',
'jax/output/SVG/jax.js?V=2.7.5',
'MathJax.js?config=TeX-AMS-MML_HTMLorMML%2CSafe.js&ver=4.1']
parentDir='\\'.join(baseUrl.split('/')[-3:]) #Parent directory from URL
for path in relativePaths: #For all files
req=requests.get(baseUrl+path) #forming url
filename=path.split("/")[-1].split("?")[0] #extracting filename out of url
directory=os.path.join(parentDir,"\\".join(path.split('/')[:-1])) #Extracting directories path for local path formation
if not os.path.exists(directory): #Creating local direcories if they do not exist
os.makedirs(directory)
with open(os.path.join(directory,filename),"wb+") as file: #Storing results into files
file.write(req.content)
Local Directory Structure Output:
Beyond iterating over a defined list of files, you could also look at a couple of other options that could take a more dynamic approach to fetch files from the CDN.
cdnjs is powered by a GitHub repository, so you could explore cloning it and extracting files (I'd recommend use sparse-checkout if you do this due to repo size) or you could look at using the GitHub API to navigate the repository an extract files: github.com/cdnjs/cdnjs/tree/master/ajax/libs/mathjax/2.7.5
We actually have an API available for cdnjs, which allows you to rather easily get all the files within a version of a library. Using that list, you could then perform a similar iterative solution to what Hamza suggested to get a copy of all the files locally: https://api.cdnjs.com/libraries/mathjax?fields=assets (annoyingly we've not yet implemented API navigation per version)
Hope that helps!
Matt, cdnjs maintainer.

How to be able to "require" a config file using browserify but not have this file included in the bundle

I have an angular application which uses browserify to modularise the Javascript components.
I have a config file which holds environment specific information which I also have as a module, so I can require it to get access to this information. For example another module can just var config = require("./config) and then use this config object to access the configuration information
However I do not want this file to be added to the bundle.js since I want it to be easily editable and no compilation to be required if the information inside it is changed.
Is there a way I can still access it using require but not have it added to the bundle?
You can parse a json file and read its contents with this: https://nodejs.org/api/fs.html

How do include javascript file on nodejs, not the require module format

Could the file not formatted with the require module is include in other file on nodejs?
I need to use the javascript file for browsers on nodejs. The file is not formatted with the required module. (That's mean that do not use module.exports=)
How the file is included or loaded in other javascript file?
I try to use node-import(https://www.npmjs.com/package/node-import),
but the module will crash when passing the absolute path.
I do not want to modify the browser javascritp file.
Thank for taking your time.

Return JavaScript as static resource from a Jar archive

Is there a way to return JavaScript that is located in a .jar archive? At the moment I have the following structure:
webapp
resources
scripts
SomeJavaScript.js
...
List of such .js files is very large. And there is a .jar file that has all such files.
In Spring config files I have:
<mvc:resources mapping="/resources/**" location="/resources/"/>
To process all static resources by my dispatcher servlet. But, I'd like it to read the JavaScript files from the .jar archive. What's the easiest way to do that?
I think writing my own controller for such purpose would not be the best option.
PS: I've found the following solution:
I'm using embedded Tomcat 7, starting it using Maven plugin.
Here's mentioned that resource files need to be under WEB-INF/lib/{\*.jar}/META-INF/resources but looking inside spring-js-resources.jar, the actual location is WEB-INF/lib/{\*.jar}/META-INF/web-resources. The generated page contains links like these:
<script type="text/javascript" src="/SomeProjectName/dojo/dojo.js"></script>
And this file is not available. What can I do to resolve the problem?
Thanks.
You can use the mvc:resources tag to expose contents of .jar files on the classpath by adding a classpath: path to the locations attribute.
I believe in your case it would be something like
<mvc:resources mapping="/resources/**" location="/resources/, classpath:/META-INF/web-resources/"/>
More information in Spring MVC documentation.

Categories

Resources