Best way to get file based on url in node? - javascript

I'm new to node and am trying to figure out what the best method is for using the URL path to read the corresponding HTML document. I know how to parse the URL, and I could just open the document with the exact name as the pathname (i.e. to open index.html, you use /index.html) but personally I think it looks sloppy to end a URL with ".html" after there is already a ".com".
Essentially, my question is: how do I match the string that I get from the URL parse to a separate string that I will use to call the html file?
My only idea is to use arrays: either const home = ['/home', 'index.html']; const about = ['/about', 'about.html'] or const paths = ['/home', '/about']; const files = ['index.html', 'about.html'] and then match the string I get to the corresponding array value. I suppose this wouldn't be too terrible, but there must be a better way, right?

Related

How to use a string value inside a javascript object path

I have extracted the following from a string and assigned it to arrPath , i.e.:
arrPath = "myArray[0]";
I also have an object path that points to a value but I need to take the value of arrPath and use this in my object path, i.e.
myPath.sheet.value.arrPath.info.status
Unfortunately this is not working for me as I basically need the path to look like the following:
myPath.sheet.value.myArray[0].info.status
in order to access the value.
I have had a look at other potential solutions but can't seem to locate one that will assist with my question.

Jquery.i18n not working when using a path of a json file

When i use jquery i18n plugin by giving the path of a json file its not working, it is printing the data-i18n value instead.
when i give the object here its working as i want.
I want to use the path method instead of writing the key-value pairs of translation objects inside html file
$.i18n().load({
'en': 'assets/i18n/en.json',
'ar': 'assets/i18n/ar.json',
}).done(function () {
$('body').i18n();
});
This made the code work.
I think the translation should only take place after the json file is loaded.
The script is going to traverse all elements inside body and replace their contents with the messages under the provided translation keys. If the key cannot be found, the initial content will be displayed as a fallback. -- source -

How to get the id of a post from a URL

I have a URL that is formatted like this:
http://localhost:3000/post/5dc07270e7179a5293d14e70
The part after /post/ contains an id as you see. I managed to obtain it by doing the following:
const path = this.props.location.pathname;
const postId = path.slice(6, 30);
Is there a better way of obtaining that id in React?
You can split on "/" and slice the result with a negative index (if the id is always the last part of the path).
console.log("http://localhost:3000/post/5dc07270e7179a5293d14e70".split("/").slice(-1)[0]);
Of course, you can preprocess that string and get id out from there, but for the brighter future you should do it another way.
An example of better use is react-router-dom since it lets you define parameters in your URL, and you can easily get that.
You can use withRouter to get the history and location details in the props. From there you can split the url by '/' and the length - 1 will be your id.
Or you can go raw javascript
let url = window.location.href.split('/')
console.log(url[url.length - 1])
If the id is not specified in the url like this
?id=yourid
Then the id must have to be at the end of the url for this to work.

Setting object to url in a custom manner

I am trying to create my own sort of "state" routing and am struggling with the manipulation of the URL. I have all the working parts for saving the state as an object and such, but I need to turn that object into a URL. So for reference, here is what my object looks like :
urlObject =
[
{"module":"module1",
"customUrl":[{"mod1":["1","2"]},{"mod2":["1","2"]}]
},
{"module":"module2",
"customUrl":[{"mod3":["true","false"]},{"mod4":["5","6"]}]
}
]
So right now I am just doing a simple
$location.search(JSON.stringify(urlObject));
To toss it in the URL. It would be neat if there were some way to format that, parse the URL formatting in my own way, so like It would change to like
/module1="module1sobject/module2="module2object"
When I say module2sobject, I mean the customUrl inside that object--so in that case it would be [{"mod1":["1","2"]},{"mod2":["1","2"]}]. I'm wondering if I could get some guidance on how to begin this process of setting and getting the object out of a url like this (specifically for use in my angular controllers).

unpacking GVariant in javascript

I have an array stored as a GVariant of type a(ss) in GSettings, that I want to use in a Cinnamon Applet. I can retrieve the value successfully using the following code:
let schema = schema_source.lookup(SCHEMA_NAME, false);
let settings = new Gio.Settings({ settings_schema: schema });
let my_value = settings.get_value('myvalue');
but I can't unpack it. As far as I can see, I will probably need to unpack it using a GVariantIter structure, but the documentation is limited, and I can't find the correct interface in the gjs API (if, indeed, it exists). Does anyone know how to do it?
Thanks!
edit:
my schema looks like this:
<key type="a(ss)" name="myvalue">
<default>[]</default>
<summary>an array of (string, string) tuples</summary>
<description></description>
</key>
For the time being I'm using an external JSON file to store settings, but it's not a 100% satisfactory solution. I suppose I could maintain two as-type variables, and keep them aligned, but there must be a way to do this properly, right?
A bit late, but my_value.unpack() works absolutely fine.
my_value.deep_unpack() will recursively unpack the arrays and their elements.
From your type of setting I guess you want to store/retrieve an array of strings? In this case, there is an easier method using Gio.Settings.get_strv(String key):
// Read the array (will create a real JS array):
let string_array = settings.get_strv("myvalue");
// Now do something with it...
// Store it:
settings.set_strv("myvalue", string_array);
Gio.Settings.sync(); // Important!
In your schema, you would then include an entry like this:
<key name="myvalue" type="as">
<default>[]</default>
<summary>Some array.</summary>
<description>An Array of strings.</description>
</key>
I use the same technique in my extension: Read/Write | Schema

Categories

Resources