简体   繁体   中英

Node.JS server side script to update JSON file

I have partially written a NODE.JS file to update the JSON file with data received from the client. The post works successfully. The Get command does not. I was wondering if there's a better way to do this? I have about 6 different callback options to write for. All different. I was wondering if there's a node.JS script already done that has all of the things I need. Or if there's a different language that would make it easier.

Here's the NODE :

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {

console.log('Request received: ');
if (req.method == 'POST') {
req.on('data', function (chunk) {
fs.writeFile("comments-data.json", chunk, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
})
});
res.end('{"msg": "success"}');
};
if (req.method == 'GET') {
req.on('data', function (chunk) {
fs.readFile('comments-data.json', 'utf8', function (err, data) {
  if (err) throw err;
  obj = JSON.parse(data);
  return data;
});
});
res.end(data);
};
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');

Here's the AJAX call:

                postComment: function(commentJSON, success, error) {
                        $.ajax({
                            type: 'post',
                            url: 'http://127.0.0.1:8080',
                            data: commentJSON,
                            success: function(comment) {
                                success(comment)
                            },
                            error: error
                        });
                },

But there's an ajax call for all sorts of things with the jquery plugin that i'm using. I need to GET, POST, PUT, DELETE, and sometimes multiple within the call.

Here's a full list of all of the callbacks i'm using:

http://viima.github.io/jquery-comments/#link-3-6

Using express you can do this much easily.

const express = require('express');
const app = express.Router();

//POST Request
app.post('/',(req, res, next)=>{
  fs.writeFile("comments-data.json", chunk, function(err) {
     if(err) {
        return console.log(err);
     }

     console.log("The file was saved!");
     res.json({'status': 'Success'})
  })
})

//GET Request
app.get('/',(req, res, next)=>{
  fs.readFile('comments-data.json', 'utf8', function (err, data) {
    if (err) throw err;
    obj = JSON.parse(data);
    res.json({'status': 'Success', 'data':data})
  });
})

As for your question regarding writing it in a different module. That is based on the pattern adopted by you. There are various nodejs patterns available eg. Controller based or classes based. It all depends on what you find comfortable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM