node.js remove string from file - javascript

I am currently designing a Node.js web server that will store requests as JSON objects and store them in a text file.
An example of said text file is this:
{"elements":[
{"email":"test#test.com","timestamp":"22:10:54"},
{"email":"foo#foobar.com","timestamp:"09:56:49"}
]}
What I want to do is be able to append a given JSON into this text file. This would be more complicated than a simple fs.append() function, because I have to first get rid of the
\n]}
that closes the JSON, and then stick on
,{"email":"INPUT EMAIL HERE","timestamp":"INPUT TIMESTAMP HERE"}\n]}
to form the new completed JSON,
{"elements":[
{"email":"test#test.com","timestamp":"22:10:54"},
{"email":"foo#foobar.com","timestamp:"09:56:49"},
{"email":"INPUT EMAIL HERE","timestamp":"INPUT TIMESTAMP HERE"}
]}
I want to do all of this without having to load the text file with fs.readFileSync(path), because that would become more difficult with each new entry. So, my ultimate issue is that I need a way to strip off (effectively reverse-append) a few characters from the end of the file so that I can append on the newly inputted elements. I've already looked over the doc (http://nodejs.org/docs/latest/api/fs.html#fs_fs_createwritestream_path_options) and I saw no function for it, but I figured there has to be a way to do it.

Related

Contentful api - getting images

I am new to contentful API but so far getting content from the API has been pretty straight forward. I have created a new space using their "blog" template and I see that in the "body" field there is an "insert media" button. I don't think I get how this is supposed to be used. When I insert an image into the "body" field, it generates a code that doesn't get rendered when I pull the content form the API. I am using a markdown parser to render the text. If you create an entry with images, these images will be available as an asset. Do I need to make a separate API call for every asset I want rendered with my entry?
When you use the Insert Media button, it should generate something such as:
![Lewis Carroll](//images.contentful.com/zz2okzf5k4px/2ReMHJhXoAcy4AyamgsgwQ/ec4998388330a939288c04558c57477a/lewis-carroll-1.jpg)
That url points to the image directly, so you don't need to do any extra calls to get the asset. The asset is an entity which contains metadata as well as the url to the asset file itself, but in this case you already have that url.
You said you are rendering the Markdown, maybe there's a problem in the code that gets generated? Could you post that?

Live updating from JSON

I have a JSON file which is dynamically and contain match info including an unique id. The JSON is divided into 3 arrays live, upcoming and recent. Since i'm quite new to Javascript i'm wondering what would be the best way to go in order to make this livescore script. I need it to be updating without refreshing browser? What is my options? Maybe someone has a snippet?
The JSON is automatically updates through another script which is connected to a cron job, so the script does not need to do anything regarding the JSON. Only retrieve and show the data.
I'm using dreamhost, which gives me access to shell, so websockets and so on is an option.
You'll need a jquery user to give you a snippit for this one, but in vanilla ecmascript 6, you use an XMLHttpRequest object to get the JSON from your server. This object can request data from the server asynchronously and is triggered by the client/browser so you can update the live match info when and as often as you like. You would just have to write a function to replace the data on the webpage with the new info when it is updated.

How to sanitize user input text so that it can be used in Javascript/JSON?

I have a web app in which I allow some large text entry using text fields. This text is saved to a database and then later it is sent back to the user as a field in a JSON response. In the browser, I attempt to simply convert it to an Object using JSON.parse, but this sometimes fails depending on what the user put in the field.
I think that right now, the text has single quotes in it, and those are breaking the browser-side Javascript before I can call JSON.parse on it.
What's the best way to sanitize this data so that, ideally, I can just parse it back to an Object with minimal cleansing after it has been saved?
This isn't a sanitization problem : you can very well put a string with quotes in JSON : the encoding simply escapes them.
Your problem is an encoding one. To build a JSON string in a browser, use JSON.stringify. To do it server side, you should use the tool provided by your (unmentionned) server side language/framework.
The awesome thing with JSON is that you do not need to sanitize anything. No matter what you feed to a JSON encoder - it will always output plain JSON. Obviously that JSON needs to be HTML-encoded in case you plan to use it within a HTML page. Depending on the JS encoder you need to ensure there's no </script> in there (e.g. by replacing / with \/).
You also do not need JSON.parse. JSON is a subset of JavaScript so you can do something like that (PHP-ish for simplicity):
<script>
var obj = <?= json_encode($whatever) ?>;
</script>
If you really want to include JSON as as tring inside JSON consider not doing it. You can just have the object itself there - no need to have a JSON string within your JSON data. But if you have this anyway it should also always work.

Retrieve a CSV file generated from a table by javascript

First, let me say that I am not familiar with the terminology so, if you see something, by all means, help me improve the wording.
What I want to do is retrieve a CSV file that is generated by a website, apparently based on a table.
The site in question has two drop boxes from which one can select the queries and then, based on `onchange=', a search is made and a table is filled.
With the table filled, a button appears, which can then be pressed and the CSV file, containing the fields, is offered to download.
After poking around with the page, I was able to find and construct the URL responsible to retrieve the CSV file. It is something like:
http://www.example.com/exportCSV.action?field1=3&field2=5
The problem is, if I try to `curl' it, a empty CSV file is retrieved, with just the headers. So, I think that the actual content must be inside the table which is filled using the normal web interface.
The last call from the javascript function that generates the CSV is:
window.open("exportCSV.action?"+fields)
Is there a way to satisfy the initial search so, when I try to curl the `CSV url' I can get a filled CSV, and not a empty one?
This rather sounds like that web site is not accepting your cURL request. Some try to limit their services to “real” browsers only.
Try using a debugging tool like FireBug to have a look at the actual data that the JavaScript sends and receives over the network.
I assume you are doing your cURL call right? Passing parameters, especially on the command line, can be a bit tricky. Make sure you escape the URL correctly, for example with single quotes:
curl 'http://www.example.com/exportCSV.action?field1=3&field2=5'
Else, the & character and possibly the question mark as well might get interpreted by your shell.

Popping up a window for the user to show some data?

I have a webapp where my users might want to get some data from me in xml format. The flow I was thinking of:
User clicks a button named "export"
I generate a long xml string using javascript.
Pop up a new window and just paste the xml string into it.
I would prefer saving the xml string into a text file for the user, but I don't think this is possible with javascript (security reasons).
Are there any other best practices for doing something like this? I guess this will work fine,
Thanks
You can add a hovering text field to the page and paste the XML into that. To make things more simple for the user, set the focus to the field and select everything (so they only have to press Ctrl+C).
If you can access server-side pages, you can generate the XML string using javascript, make an ajax call to give the server your string, then make the user download the generated file.

Categories

Resources