I am using path.js. I have a service like /foo/bar/baz and I use baz as parameter. So my router is like:
Path.map("#/foo/bar/:baz").to(function(){
//call the service
});
However sometimes I need a url like: /foo/bar/baz/ignore/whatever/etc/bla/bla. But /whatever/etc... part is not predictable as we may have more than one parameter.
So is there a possible way to get rest of url as a parameter after a certain level. I need something like:
Path.map("#/foo/bar/:baz(/*rest)").to(function(){
var params = this.params['rest'] ? this.params['rest'] : this.params['baz'];
});
in this case rest = ignore/whatever/etc/bla/bla
Related
There is an address of the type site.com/userlist/?getlist=XXXX&userid=XX. I need to get part of the URL, namely /userlist/?getlist=XXXX&userid=XX and insert the button with the ID reg_button.
I'm leaning towards jquery and apparently regex, but I haven't found a suitable method yet or I don't understand it, I'm just learning)
let url_test=new URL('http://www.yourtestdomain.com/userlist/?getlist=XXXX&userid=XX');
console.log(url_test.pathname+url_test.search);
This takes the url minus base (domain) aswell as the query part, and also URN if exists
documentation
console.log(window.location.href.replace(window.location.origin, ""))
There are many ways to do it. I have included 2 ways here. URL class can also be used as mentioned by one of the answerer but the URL string needs to be complete with http prefix.
If you want it using regex you check this Stackoverflow thread.
var siteurl = "site.com/userlist/?getlist=XXXX&userid=XX";
function method1(){
var paths = siteurl.split('/');
paths.shift();
var basePath = paths.join('/');
alert(basePath);
}
function method2(){
var basePath = siteurl.substring(siteurl.indexOf('/'));
alert(basePath);
}
<button onclick = 'method1()'>Method1</button>
<button onclick = 'method2()'>Method2</button>
I am trying to pass parameters from my website to a couchdb server through a node.js server.
I absolutely need to pass {} in a url. Not a string, not an empty object, the actual {} characters. It is used to define the end_key parameter in couchdb views.
At the moment, my call goes like this :
let url = "/trades";
let ajax_options = {
data:{
design_name:'bla',
view_name:'blabla',
params_view:{
group_level:2,
start_key:["1",0],
end_key:["1",{}]
}
}
};
$.ajax(url,ajax_options).then((res) => { ... });
when it passes through NodeJs and the nano library with
db.view(req.query.design_name, req.query.view_name, req.query.params_view)
the end_key object in params_view becomes ["1"] instead of ["1",{}] which I would like to see.
I have verified that with the correct value for end_key, the view gives me the expected result.
How to prevent that behavior from occurring ?
I want to add multiple parameters with the same name to a request URL. I'm using Angular's $http.
The URL should look like this:
http://myBaseUrl?name1=value1&name1=value2...
I know that it is possible to make something like this when I set the values as an array:
http://myBaseUrl?name1=value1,value2...
But it has to be like the first one.
If you're using HttpClient you can use HttpParams for this.
let params = new HttpParams();
// Assign parameters
params = params.append('firstParameter', 'valueOne');
params = params.append('firstParameter', 'valueTwo');
// Get request
this.http.get(`http://example.com`, { params }).subscribe();
Questions
How to serve javascript file dynamically? Specifically, the scripts maintain most of its body but with some variables changable (imagine HTML Jade template, but this is for pure javascript).
Scenario
When user or browser (http GET in general) visits /file.js passing parameter api, e.g. /file.js?api=123456, I would like to output pure javascript where I can take that 123456 and put in inside of my code, dynamically. Content-Type is application/javascript.
Sample:
var api = #{req.query.api}; //Pseudo
//The rest of my javascripts template
...
From my main .js file, I have set up the route:
app.get( '/file.js', function( req, res ) {
//Pseudo code that I would like to achieve
var name = req.query.name;
res.render( 'out_put_javascript_file_from_jade_file.jade', { name: name } );
});
So when a person visits /file.js, the script file will be rendered differently based on the parameter api passed in the URL. The only possible dynamic way I can think of is using Jade, but it doesn't allow pure javascript template. I believe there must be other solutions.
Please excuse my explanation. The problem is somewhat like this: How to generate a pure JavaScript file with Jade
If you want to do something quick and dirty, then you can do something like this (based on your example in the comments).
App init - read the .js template file and cache it:
// this should be async, but hey, not teaching you that part here yet
var fileJs = fs.readFileSync('file.js.template');
File.js:
(function() {
$(window).on('load', function() {
alert('Your api key is API_KEY_CONST');
});
})();
Request:
GET /api/file.js?key=123
Router:
app.get('/api/file.js', function(req, res) {
var key = req.query.key;
var key = fetchKeyFromDBSync(); // just to make it easier here, no async.
var out = fileJs.replace(API_KEY_CONST, key);
res.setHeader('content-type', 'text/javascript');
res.write(out);
res.end();
});
Now, this is really dumb and you should not try it at home, but it simply demonstrates how to do what you wanted.
Edit:
Depending on the file length, you might perform a bit better if you put the chunks of the file into an array, like:
var fileChunks = ['(function(){ blablabla;', 'var myAPIKey=', 'KEY_PLACEHOLDER', '; alert (myAPIKey);', '})()']
So later when you're resolving it with the real API key, you join the file.
fileChunks[2] = '12345';
var responseData = fileChunks.join('');
res.write(responseData);
But your last-accessed api key is then held in an array. Not quite future proof, but it shouls work if you need something quick.
I'm following Jame Yu's Backbone tutorial here to create my own app. Below is my model. I wonder if there's a way to generate separate urls for edit and update (RESTful) instead of just 1 as in the tutorial. I use Rails on the back end. Thanks.
var BusinessCard = Backbone.Model.extend({
url : function() {
var base = 'business_cards';
if (this.isNew()) return 'backbone/' + base;
return 'backbone/' + base + (base.charAt(base.length = 1) == '/' ? '' : '/')
+ this.id;
}
})
You are missing the point of REST...the fact that there is one URI which responds to different verbs from the uniform interface (GET, POST, PUT, DELETE), makes it restful. So Backbone is actually being RESTful, while you're not.
The default backbone sync method works exactly how you want it to already by appending the models id to the url when performing an update.
If you need to customize how data is sent to your server I've found the best thing is to create your own backbone sync. Here is an example of how I do it to wrap my create and update requests in a root json object: https://github.com/codebrew/rails3-backbone-coffeescript/blob/master/app/coffeescripts/lib/mongo_model.coffee