Parse.com Node js failed to load json file - node.js

I'm trying to read a json file using node js/ express and deploying it to parseCloud but i keep getting
*Failed to load filters.json with: Could not find file filters.json *
here is my code:
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('cloud/filters.json', 'utf8'));
or this
var filterJson = require('cloud/filters.json');
thanks

Looks like parse.com doesn't allow .json files. You can save your file as .js and load it as plain text file (doesn't work with require()).
var fs = require('fs');
var parsedObject = JSON.parse(fs.readFileSync('cloud/path/json_file.js'));
It looks ugly, but works for me. :)

Try to add ./
fs.readFileSync('./cloud/filters.json', 'utf8')

Related

Encoding issue with reading markdown files on Github

When I load a markdown file from GitHub I am running into a lot of errors. I think I am not using the right encoding for GitHub files through Octokit. Any suggestions on how to fix my Buffer code in Node.js?
Is base64 and then to ascii correct for Github content? It works fine when loading it directly in my project without GitHub. I have a feeling GitHub stores their files in a different format but can't find docs on it.
const repos = await octokit.repos.getContents({
owner: 'owner-hidden',
repo: 'repo-hidden'
path: '/dinner.md
});
// repo loads with data.content just fine
const bufferedData = Buffer.from(repos.data.content, 'base64').toString('ascii');
const ymlData = YAML.parse(bufferedData); ## issue with reading this
The error is below, but the error doesn't necessarily matter because it works when I load it directly in my project there are no errors.
YAMLException: the stream contains non-printable characters at line 36, column 126:
... auteLed spinach and ratatouille
^
Loading the markdown file directly in my project there was no errors:
const fs = require('fs');
const path2 = require('path');
const file = path2.resolve(__dirname, '/dinner.md');
const content = fs.readFileSync(file);
const bufferedData = Buffer.from(content).toString('ascii');
console.log({bufferedData});
As one of the members of Octokit replied to me on my Github issue, I don't need to encode with ascii, I should be using uft8 as shown here:
- const bufferedData = Buffer.from(repos.data.content, 'base64').toString('ascii')
- const bufferedData = Buffer.from(repos.data.content, 'base64').toString()
buffer.toString() defaults to utf8 which is what I want.

How to render a Log / text file in node js through url?

I am trying to view the log File in the browser on hitting a particular url. I have the log file in my local (/logsFolder/app.log)
I tried the following codes:
Code: 1
var app = express();
var path = require('path');
var logFile = require('/logs/app');
app.use('/logs',logFile);
It threw error like
Error: Cannot find module '/logs/app'
Code :2
const app = express();
const path = require('path');
const router = express.Router();
router.get('/logFile', function(req, res){
console.log("inside logt :");
res.render('./logs/app.log');
});
can anyone Please help me to resolve.
Your log is static text file, not a javascript, nor json file, that why you can't require it. (code 1)
You are not using template engine either, that's why your code 2 didn't work, It cannot be render by itself.
You can use the built in express middleware for static files.
Try this:
app.use(express.static('logsFolder'))
Now you can access all the content of logsFolder by requesting the file name. For example: http://your-url/app.log
Or try your code 2 with res.sendFile instead of res.render

How to download a .gz file with Node.js without any third party libraries

I simply want to download a .gz file from a URL and save it in a folder. I would like to do this without any third party libraries if possible. Here's what I have so far, but it only downloads an empty file:
const fs = require('fs')
const https = require('https')
let file = fs.createWriteStream('./folder/filename.gz')
let request = https.get('https://someurl/somefile.gz', function(res) {
res.pipe(file)
})
you can try this, using HTTP module for nodesJS,it looks similar to downloading any other file, just remember to mention the extension of the downloaded file when calling instead....Here is an example:
NOTE: IF you are trying to download from an HTTPS link, use the HTTPS
module instead, its exactly the same, but just replace all the
HTTP in the following code with HTTPS
const http = require('http');
const fs = require('fs');
//I added './' assuming that you want to download it where the server
//file is located, just change it to your desired path, followed by the
//filename and the EXTENSION
const file = fs.createWriteStream("./result.tar.gz");
const request = http.get("http://alpha.gnu.org/gnu/gzip/gzip-1.3.6.tar.gz", (response) => {
response.pipe(file);
});

Node.js reads the file but does not write JSON in the HTML

I'm currently running Node.js by Browserify for my website.
It reads the JSON file and I get the message through MQTT.
But the problem is that it seems like writefile does not work.
(Running this as node test.js in the terminal works by the way).
What is wrong with my code?
Moreover, Is this the best way to store any user data?
Thank you so much in advance.
Here's some part of my code
var fs = require("fs");
var path = require("path");
let newFile = fs.readFileSync('/home/capstone/www/html/javascript/test.json');
function testT() { //THIS WORKS FINE
let student0 = JSON.parse(newFile);
var myJSON = JSON.stringify(student0);
client.publish("send2IR", myJSON);
response.end();
};
function write2JSON() { //PROBLEM OF THIS CODE
const content = 'Some content!'
fs.writeFileSync('/home/capstone/www/html/javascript/test.json', content)
};
document.getElementById("blink").addEventListener("click", publish);
document.getElementById("write").addEventListener("click", write2JSON);
You cann't write directly for security reasons. For other hand you can use a server as API to do the filye system tasks and in the client only trigger the events.
This post is very related with your problem:
Is it possible to write data to file using only JavaScript?

NodeJS Read File Name is Japanese Characters

I have a file name is momo1.json. I can read them in Nodejs
var fs = require('fs');
var jsonWelcome = fs.readFileSync(WELCOME_JSON, UTF8_FORMAT);
var dataWelcome = JSON.parse(jsonWelcome);
But now i change file name is ももたろう.json. I cant read file anymore, tell my why and solutions i can read file name by japanese charaters
Try to use require() instead:
var dataWelcome = require("./ももたろう.json");
//output the data to see if it works
console.log(JSON.stringify(dataWelcome));
Live Demo

Resources