Handling assets in node.js with express - node.js

ok so what I am trying to do is manually handle my assets when using express. What I mean is that I do not what to have to have every stylesheet/javascript file on every page. So I wanted to be able to specify in each route whether I wanted to use another javascript file or not. Ex:
app.get('/testing',function(req,res,next){
assets.addJS('my-javascript-file');
res.render('testing');
});
So now when the template goes to render I want all of those javascript files that have been added in a local variable. I do not want to pass them on each call to render because I will want to add javascript in other places and may not necessarily need to send anything to the template which would cause me not to pass it by accident. Another thing that I want to implement is caching. I know by default it sends back the 304 not modified once it has the asset but I dont even want it making that request so I was hoping to do a query string on the files when they are output with maybe the last time they were modified and that way if I change them it will automatically tell the users browser to get the newest files but other than that it will cache them saving me bandwidth / requests (amazon s3 charges for these). If anyone can point me in the right direction or if there is already a plugin out there for this please let me know. Thanks.

You could build an asset manager around this:
app.get(function(req, res) {
res.locals.files = [ 'somefile.js', 'somefile2.js' ];
res.render('someview');
});
The view would just create multiple script files.
head
each file in files
script(src=base_url + '/' + file)
If you want to merge the files into one request you probably need to extend this even more. You could create a route that can take an array of files and merges them on each request. You can serve files with res.sendFile. This will take care of all the headers and caching if I remember correctly.

Related

How to append into txt file through karate.write(value,file.txt) function? [duplicate]

In my karate tests i need to write response id's to txt files (or any other file format such as JSON), was wondering if it has any capability to do this, I haven't seen otherwise in the documentation. In the case of no, is there a simple JavaScript function to do so?
Try the karate.write(value, filename) API but we don't encourage it. Also the file will be written only to the current "build" directory which will be target for Maven projects / stand-alone JAR.
value can be any data-type, and Karate will write the bytes (or plain-text) out. There is no built-in support for any other format.
Here is an example.
EDIT: for others coming across this answer in the future the right thing to do is:
don't write files in the first place, you never need to do this, and this question is typically asked by inexperienced folks who for some reason think that the only way to "save" a response before validation is to write it to a file. No, please don't waste your time - and please just match against the response. You can save it (or parts of it) to variables while you make other HTTP requests. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice. Also note that by default, Karate will dump all HTTP requests and responses in the log file (typically in target/karate.log) and also in the HTML report.
see if karate.write() works for you as per this answer
write a custom Java (or JS function that uses the JVM) to do what you want using Java interop
Also note that you can use karate.toCsv() to convert JSON into CSV if needed.
My justification for writing to a file is a different one. I am using karate explicitly to implement a mock. I want to expose an endpoint wherein the upstream system will send some basic data through json payload using POST/PUT method and karate will construct the subsequent payload file and stores it the specific folder, and this newly created payload file will be exposed through another GET call.

p:remotecommand to upload files

I would like to upload file programatically to my jsf application. The user should select a directory on his system, and a js script should loop on any file in dir and send each one to the listener serverside
I cannot use FileUpload, because it cannot select a whole dir with thousands of file, so I was thinking to use jquery and send the file to a remotecommand, but I have no clue to how send the file itself (normally I pass just string)
so I was thinking to use jquery and send the file to a remotecommand, but I have no clue to how send the file itself
Don't go there. It is a bad attempt to a workaround for a bad design choice. You'd most likely run into similar problems and what about the user selecting a lot of files for second time if it fails halfway? It might become slow, might run into browser limits (search for uploading multiple files in plain html)...
If you still want to do it via a webbrowser (which according to one of your other questions you do not want to), maybe try something like https://webdeltasync.github.io/ (disclaimer: I did not use it myself, and there might be similar ones (https://www.google.com/search?q=browser+based+rsync), it is just a hint in which direction to find a real solution)

Google tells do not change dynamic URLs, and offers this instead, how?

If you want to serve a static equivalent of your site, you might want to consider transforming the underlying content by serving a replacement which is truly static. One example would be to generate files for all the paths and make them accessible somewhere on your site.
What they mean exactly? And how to do it?
Your question: What do they mean exactly?
If you want to serve a static equivalent of your site - static refers to html pages that are not dynamically created.
you might want to consider transforming the underlying content by serving a replacement which is truly static. Have 'hard copies' of your pages with the different alternatives
One example would be to generate files for all the paths and make them accessible somewhere on your site. Go through your site and create static html pages (or pdf's) of each one and store them in the file structure that is represented by the URL.
Example of the last:
http://site.tld/product/pear which today is a dynamic (created on the fly by the code and database) but is not really in an actual folder on the server called product. They are suggesting to create a copy of the dynamically created page and store it in an actual folder on the server called product with the name pear.
Your question And how to do it?
Will that work - sort of if you wanted to by adding a .html to the physical file (copy of the dynamic one) and save it but I suspect you will run into all sorts of difficulties that you will need to overcome with the redirect code in places like .htaccess. Another option may be change the domain part of the URL to include static ie http://static.site.tld/ for the static copies and the original URL as is for the dynamic version.
The other big challenge then becomes maintaining the two copies because the concept they talk about is for the content (what is shown in the browser) to remain static over time. Kind of breaks the whole concept of how we build dynamic web sites today e.g. online shops etc.
For example if it's a shop, I would use PHP to also create the physical file when a product is added and not include parts that are going to change, rather include a link to the dynamic info something like:
<?php
$file = 'product/pear.html';
// mysql code here to extract the info and format ready for writing
$content = "<html><head><title>$title_from_db</title></head><body>$page_content_from_db</body></html>";
// Write the contents to the file
file_put_contents($file, $content);
?>

Is it possible to app.post once for multiple jade file in the same directory using node.js?

I am trying to use node.js to create a program that stores a task, each task is dynamic: such as adding two random number, so I store the task as a jade file, since the second time you called, the random number might be different
The problem I am having right now is that I can write the app.post for each task, but I don't want to do that as there might be ten jade files in that directory, and I have to write app.post for each task ten times, which is not scalable, so I was wondering if there is a way to run all the jade file using one app.post?
Any hint would be appreciated
Use app.get('/:task', do) for routing and req.params.task to get the value passed from client.
See http://expressjs.com/api.html#req.params for more information.

Edit and modify index.dat windows file

In order to be able to speed up user logging times for our product we are trying to implement a preloaded user session scheme. In order to be able to migrate browsing preferences from the preloaded user to the real one we need to be able to edit the contents of the C:\Documents and Settings\User1\Cookies folder: cookie files + index.dat.
In order to achieve this we tried to replace all occurrences of “User1” with say “User2” for cookie files and index.dat. Since both user names have the same character length this change should no break the binary integrity of index.dat file. However after performing this change the cookies no longer work .. Any suggestion would be appreciated.
Editing index.dat seems hopless. The format has never been made public. Trying to reverse engineer it has only resulted in limited sucess: http://www.latenighthacking.com/projects/2003/reIndexDat/
However there are the Microsoft APIs: FindFirstUrlCacheEntry, FindNextUrlCacheEntry, InternetGetCookie, InternetSetCookie that can be used to export and import cookie data.

Resources