How to save JSON file with Node.js

Asked

Viewed 7,390 times

4

I want to make a website to test some things. The site is running on my home server done with Wamp. I was told that I could make a small database using JSON files. I have a JSON file in the website folder that contains the following:

[
        { 
            "titulo":"Filme A",
            "titulo original":"Movie A",
            "duracao":"120 min",
            "genero":"Genero 1",
            "Atores":"Nome 1, Nome 2",
            "Rate":"1",
            "Visto":"sim"
        },
        {
            "titulo":"Filme B",
            "titulo original":"Movie B",
            "duracao":"160 min",
            "genero":"Genero 2",
            "Atores":"Nome 1, Nome 2",
            "Rate":"2",
             "Visto":"sim"
        },
        {
            "titulo":"Filme C",
            "titulo original":"Movie C",
            "duracao":"140 min",
            "genero":"Genero 3",
            "Atores":"Nome 1, Nome 2",
            "Rate":"",
             "Visto":"nao"
        }
]

With your help I was able to list the properties of these objects from the JSON array.

Now I’m trying to save a new object inside this JSON array through information that the user puts when the questions are asked and for that I need a scripting on the server side, I chose Node.js because I’m a little more familiar with Javascript. I have little knowledge and almost none of Node.js. But I would still like any help from you to try to learn something. I have read on the internet about Node.js and JSON but still don’t understand how to save JSON on the server using Node. Any explanation will be very helpful.

See on Jsfiddle

2 answers

4


The most indicated, which was cited by Joel is indeed to use Mongo, but if you are interested in using a file manipulation API on Node.JS, you can use the Filesystem API, see an example below

var fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
}); 

Or as the example below, it could be using Node.JS with connection to the Mongodb

var MongoClient = require('mongodb').MongoClient,
Grid = mongo.Grid;

// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
    if(err) return console.dir(err);

    var grid = new Grid(db, 'fs');
    var buffer = new Buffer("Hello world");
    grid.put(buffer, {metadata:{category:'text'}, content_type: 'text'}, function(err, fileInfo) {
        if(!err) {
             console.log("Finished writing file to Mongo");
        }
    });
});
  • I didn’t quite understand what the Filesystem API would be. Enter the code you showed first inserts "Hey there!" in the base date, but what base date is this? It’s some JSON file?

  • In fact he is only writing in the file the phrase "Hey There!" in a physical file and not in bank.

2

  • I was actually reading on this site, but I have a problem installing Mongoose. I managed to install Mongodb and start it quietly. But install Mongoose not. I did as directed on the link you gave me but says it is not in the npm record. I googled Mongoose and found one that seemed to me but when I started the following mistake: "Falied to set option [listening_port]: Cannot bind to port"

  • Hmmm should give! This is listed in npm: https://npmjs.org/package/mongoose

  • Sorry, I typed the command in a new prompt and it worked. On the link you gave me, the author connects the DB referring mongodb://localhost/test "Mongodb DB test". In my case the test was in data db. I have to change the Mongo DB location to my www folder on the wamp server?

  • Should this code be written or called when the page finishes loading? @Joelcunhaferreira

  • 1

    Could you give details on the matter? Answers with links only do not survive the time that is our goal: http://meta.pt.stackoverflow.com/questions/42/queremos-respostas-que-contenham-somente-links

Browser other questions tagged

You are not signed in. Login or sign up in order to post.