Navigate/Search

Archive for the 'Node.js' Category

Barter Application… On Hold

Saturday, August 9th, 2014

I’ve gotten very frustrated with working on my Barter Application.  The problem is that I am spending a lot of time trying to get Angular.js and Node.js working together RESTfully.

I suppose anyone reading this is thinking one of two things:

1) Wow, that sounds very hard… almost impossible.  Only the most brilliant minds can do that.

2) That’s easy.  Heck I could do that in my sleep.

I need to move from group one to group two.  So I am going to work through a book called “Node.js, MongoDB, and Angular.js” in an attempt to get a little stronger from a Node.js/Angular.js perspective.  Will see how it goes.

Found a fix for my “Cross Origin Resource” Error

Wednesday, July 23rd, 2014

Before I left for Australia I was working on an application.  The 30,000 foot technical description of the application is that there is a HTML, JS, and CSS code base.  The JS portion makes use of the Angular.js library… that’s kind of the point of the whole project, to learn Angular.js.

I want to develop my code on my local machine and I don’t want to run my local code through a web server.  In my local application I want to use node.js and flat json files.  My production version will run through a php server and the PHP will generate json data based on data in a MySQL database.

My first road block came on my local development system.  The problem was that when I tried to access the JSON file via node.js I was seeing a strange error about “Cross Origin Resource” and it was unclear to me where this error was being generated.

In a professional environment this kind of error is a real pain in the butt because the front end people say it’s a back end issue and the back end people says it’s a front in issue.

So… this is a back end issue.  It’s a security issue, node.js (unless told otherwise) rejects requests from other origins… whatever that means.

I had to add some code on the node.js side.  Here’s the code:

var headers = {};
headers["Access-Control-Allow-Origin"] = “*”;
headers["Content-Type"] = “*”;
res.writeHead(200, headers);

That caused my problem to go away.  The truth of the matter is that this is in many cases a serious security risk.  I am runnin node.js on my personal machine and my personal machine is behind a router and there is no way to access my “localhost” from the outside and even if it could be accessed in some way, it only serves a few flat JSON files.  So it’s cool for my node.js server to allow all (*) remote requests.

 

 

Barter Website: Node.js loading the JSON file.

Thursday, July 3rd, 2014

I updated my node.js development server to load my flat JSON file.

var sys = require(“sys”);
var http = require(“http”);
var path = require(“path”);
var url = require(“url”);
var fs = require(“fs”);
var qs = require(‘querystring’);
http.createServer(function (req, res) {
console.log(“Received request: ” + req.url);
var pt = url.parse(req.url).pathname;
var qy = url.parse(req.url).query;
var qyList = [];
if(pt == “/api/item”){
res.writeHead(200, {‘Content-Type’: ‘application/json’});
var index = fs.readFileSync(‘item.json’);
res.end(index);
} else {
res.writeHead(404, {“Content-Type”: “text/plain”});
res.write(“404 Not Found\n”);
res.end();
}
}).listen(1338, ’127.0.0.1′);
console.log(‘Server running at http://127.0.0.1:1338/’);

The next thing to do is to set my angular.js code up to get the data via AJAX. That should be interesting.

Node.js code for my barter website.

Wednesday, July 2nd, 2014

I am using Node.js as my development server.  The dev server can be pretty simple because all it needs to server up is example data.  My plan is to hold my data in JSON text files and server them.  At this time there is only one file to be served and that file should only be served by one URL.  All other URLs should server 404 errors.  Here’s the code:

var sys = require(“sys”);
var http  = require(“http”);
var path = require(“path”);
var url = require(“url”);
var fs = require(“fs”);
var qs = require(‘querystring’);
http.createServer(function (req, res) {
  console.log(“Received request: ” + req.url);
  var pt = url.parse(req.url).pathname;
  var qy = url.parse(req.url).query;
  var qyList = [];
  if(pt == “/api/item”){
      res.writeHead(200, {‘Content-Type’: ‘text/html’});
      res.write(pt);
      res.end();
   } else {
      res.writeHead(404, {“Content-Type”: “text/plain”});
      res.write(“404 Not Found\n”);
      res.end();
   }
}).listen(1338, ’127.0.0.1′);
console.log(‘Server running at http://127.0.0.1:1338/’);