How do I run a Frisby.js test inside a function - javascript

I can't figure out why this frisby tests won't run!
Basically I'm trying to import JSON from a file and check it against a return from a request. The compiler doesn't seem to find any tests when I run this file.
If anyone could possibly suggest a better way to do this? I'm thinking about trying a different way to handle the file reading. I know about readFileSync() but I do not want to use that if I don't have to! Any help would be appreciated.
function readContent(callback,url,file) {
fs.readFile(file, 'UTF8', function (err, content) {
if (err) return callback(err)
data = JSON.parse(content)
callback(null, data)
})
}
readContent(function (err, content) {
frisby.create('Testing API')
.get(url)
.expectStatus(200)
.expectBodyContains(content)
.toss()
},
url,file)

Here's one I prepared earlier:
// Read a JSON file from disk, and compare its contents with what comes back from the API.
fs.readFile(path.resolve(__dirname, 'GET_ReferenceTypes.json'), 'utf-8', function(error, data){
if (error) throw error
frisby.create('GET ReferenceTypes, inside readFile callback')
.get(URL + 'ReferenceTypes?requestPersonId=2967&id=99')
.expectStatus(200)
// JSON.parse() is required to convert the string into a proper JSON object for comparison.
// The .replace() strips the BOM character from the beginning of the unicode file.
.expectJSON(JSON.parse(data.replace(/^\uFEFF/, '')))
.toss();
});
Double check the encoding on your JSON file, because this whole thing comes apart without the .replace() call.

Related

How to read multiple objects from text file using node?

I have a text file, which contains multiple objects written to it. I need to fetch all the objects as JSON in the text file. What should I do?
data in my file:
{"events":[...] },{"events":[....]},{},{}....
I tried to read it as :
fs.readFile('gcyoi6.txt', function (err, data) {
if (err) throw err;
data =data.toString();
console.log(data)
});
it gives the data as a string. But I need it as JSON objects
Thanks in advance!
You can pass the data which you get in the file to JSON.parse function which will converte string get from file into a JSON representation of the content which is in your .txt file.
fs.readFile('gcyoi6.txt', function (err, data) {
if (err) throw err;
data =JSON.parse(data);
console.log(data)
});
This is a valid text which can convert to JSON
const validJSONString = JSON.parse(`[{"event":"name"},{"event": "test"}]`);
console.log(validJSONString);
This is a bad JSON
const invalidJSONString = JSON.parse(`[{"event":"name"},{'event': 'test'}]`); // Throw an error error

Find if a string is in a json file, then get the stuff around it

So I don't know a lot about JSON, I'm making a bot for a game that you can message and it sends you a code that you then send to my discord bot to link your account. I'm doing this by making it so whenever the bot is friended in game it accepts the request and sends a message to the user with a 5 Character code that is randomly generated and then stored in users.json with their player ID and Displayname. I'm done with the bot part mostly just the discord part now, I need to find if the code they input is in the json file and then get the displayName that goes with that code. If that makes sense...
Here's users.json with only one entry:
{"users":[{"name":"ImBattleDash","Id":"34a02cf8f4414e29b15921876da36f9a","code":"5MJS3"}]}
and here's the code to add to it:
fs.readFile('./users.json', 'utf-8', function(err, data) {
if (err) throw err
var arrayOfObjects = JSON.parse(data)
arrayOfObjects.users.push({
name: userAuthorName,
Id: userAuthor,
code: authorCode
})
console.log(arrayOfObjects)
fs.writeFile('./users.json', JSON.stringify(arrayOfObjects), 'utf-8', function(err) {
if (err) throw err
console.log('Done!')
})
What I basically need is to search in the Json file for example if one of the codes is "5MZJ2" and if it is then get the name that goes with that and set it to a variable.
I've been trying to figure out how to do it but I'm still quite a beginner so some help would be appreciated!
A quick way to do it is to use the find() method, a solution would be:
let myCode = "5MJS3"
fs.readFile('./users.json', 'utf-8', function (err, data) {
if (err) throw err
var arrayOfObjects = JSON.parse(data)
let myEntry = arrayOfObjects.users.find(entry => entry.code == myCode)
console.log(myEntry.name)
})
It basically go through your list and looks for an entry mathing the code :)

Will fs.readFile() cache the file's contents in the server's memory after the first read?

I would like to know if the following code would cache the file's contents in the server's memory after reading it once. The reason I ask is because I don't want to have to re read the file every time the user requested the page. I would prefer to have it cached after the first read.
fs.exists(fileName, function (exists) {
if (!exists) {
console.log("== 404 error");
resp.writeHead(404, {'Content-Type': 'text/html'});
resp.end(pageError);
return;
}
fs.readFile(fileName, 'utf8', function (err, data) {
if (err) {
resp.writeHead(404, {"Content-Type": "text/html"});
resp.end(pageError);
return;
}
var contentType = getContentType(req.url);
var mimeType = mimeTypes[contentType];
resp.writeHead(200, {"Content-Type": mimeType});
resp.end(data);
});
});
NOTE ** I only want to know how to do this using internal Node JS modules (no express)
You shouldn't use fs.exists() as its deprecated; instead, use fs.stat() if you only want to check existence. If you are going to open and read a file after checking for existence, then just use fs.readFile() and handle the passed error accordingly for a not existing file. This is noted within the fs docs for fs.access() but still applies to fs.stat() as well. Below is the excerpt from the Node.js docs.
Using fs.access() to check for the accessibility of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.
fs.readFile() does not do any caching for you, this is something you'll need to create/manage yourself. The below example shows how to create a file cache using a JS Object as a dictionary to keep the file contents indexed by filename. Its important to note that you shouldn't be putting gigs of data in the fileCache object, instead this will be good for lots of smaller files.
fileCache just needs to be in scope of getFileFromCache() and in a place that won't be garbage collected during runtime.
const fileCache = {}
const getFileFromCache = (filename, cb) => {
if (fileCache[filename]) {
return cb(null, fileCache[filename])
}
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
return cb(err)
}
fileCache[filename] = data
return cb(null, data)
})
}
Will fs.readFile() cache the file's contents in the server's memory after the first read?
No. fs.readFile() itself does no caching.
But, the underlying operating system will do file caching and as long as there isn't so much other file activity going on that the cached read gets flushed, then the OS will probably fetch the file from a local memory cache the 2nd, 3rd times you read it.
If you want to assure the caching yourself, then you should just store the contents yourself once you first read it and from then on, you can just use the previously read contents.
You could store the file data in a variable, or in a global variable (by using global.<varname> = <filedata>) if you want to access it across multiple modules.
Of course, as George Cambpell said, anoy modification to the file won't be noticed by your program, since it won't re-read the file.
So I would do something like this:
function sendResponse(data) {
let contentType = getContentType(req.url);
let mimeType = mimeTypes[contentType];
resp.writeHead(200, {"Content-Type": mimeType});
resp.end(data);
}
if(global.fileData) {
return sendResponse(global.fileData);
}
fs.readFile(fileName, 'utf8', function (err, data) {
if (err) {
resp.writeHead(404, {"Content-Type": "text/html"});
resp.end(pageError);
return;
}
global.fileData = data;
sendResponse(global.fileData);
});
The first time global.fileData will be empty, so you'll proceed with fs.readfile, store the file content in global.fileData, and send the response.
The second time global.fileData will contain stuff, so you just send the response with that stuff, and you won't read the file again.
For further reference take a look at the official NodeJS documentation: https://nodejs.org/api/globals.html#globals_global
Another thing you should do is replace fs.exists with fs.access or fs.stat (I usually use fs.access), because the exists method is deprecated.
https://nodejs.org/api/fs.html#fs_fs_stat_path_callback
https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback
Happy coding!

Cannot read file with nodejs

I use the following code to read a file from my desktop. When I run the server and use some request I don't see anything in the debugger.
What am I missing here?
fs = require('fs');
fs.readFile('‪C:\Users\i123\Desktop\test.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
res.send(data);
});
It's hard to know all the things that might be wrong here since you only show a small piece of your code, but one thing that is wrong is the filename string. The \ character in Javascript is an escape mechanism so that the string '‪C:\Users\i123\Desktop\test.txt' is not what you want it to be. If you really need backslashes in the string for a Windows filename, then you would need to use this:
'‪C:\\Users\\i123\\Desktop\\test.txt'
Other things I notice about your code:
Returning a value from the readFile() callback does nothing. It just returns a value back into the bowels of the async file I/O which does nothing.
When you get a file error, you aren't doing anything with the res which presumably means this route isn't doing anything and the browser will just be left waiting.

Node xml2js is returning 'undefined' when using parseString()

I'm using this package elsewhere and it's working just fine, however in one particular example with one XML file I'm getting "undefined" errors.
Example:
fs.readFile('./XML/theXMLfile13mb.xml', 'ascii', function(err,data){
if(err) {
console.log("Could not open file " + err);
process.exit(1);
}
parseString(data, function (err, result) {
console.log(result); // Returns undefined
var json1 = JSON.stringify(result); // Gives an error
var json = JSON.parse(json1);
The xml2js docs don't really mention how this might be possible/what this might mean. I've tried using other XML files and they work fine. This particular XML file is no bigger than the others nor does it appear to be any less in-tact (it opens fine in my browser and all the data is presented as expected).
Any ideas on how I could troubleshoot this?
You need to convert the data from a Buffer to a String, use this:
parseString(data.toString(), function (err, result) {
Instead of:
parseString(data, function (err, result) {

Categories

Resources