1

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

Grant Campbell
  • 143
  • 2
  • 12
  • There's [Express](http://expressjs.com/), which would make it a lot easier to set up the routes. – adeneo Aug 10 '16 at 19:46
  • Okay not opposed to that. Could you provide an example? – Grant Campbell Aug 10 '16 at 19:47
  • http://stackoverflow.com/questions/9577611/http-get-request-in-node-js-express – Daniel Lizik Aug 10 '16 at 19:49
  • If you add the [body-parser](https://github.com/expressjs/body-parser), it would just be something like `app.get(function (req, res) { fs.writeFile("comments-data.json", req.body);` etc. – adeneo Aug 10 '16 at 19:50
  • @adeneo that snippet in place of what? – Grant Campbell Aug 10 '16 at 20:02
  • You're going to have to read some of the documentation for Express. In general when you do `app.get('url'` you're listening for a GET request to a certain URL, and likewise `app.post('url'` listens to a POST request etc. Using the bodyparser you can get the data you sent more easily etc. but again, you have to look at the docs, and try it out – adeneo Aug 10 '16 at 20:05
  • Would it really be faster to re-write what I have then if i'm using just a different module inside Node.JS? – Grant Campbell Aug 10 '16 at 20:43

1 Answers1

0

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.

Mr Khan
  • 2,139
  • 1
  • 7
  • 22