title: Node.js API returns JSON in terminal but not in browser
body: <p>Having a strange problem. Been searching for answers but nothing turns up. I'm doing a node api tutorial and it returns JSON from my mongoDB database in my terminal when I perform any GET request but in my browser or postman I get nothing back, only in the terminal do I get any response. When I try a POST in  postman it says it can't connect to the backend. </p>
  
  <p>here is my code : </p>
  
  <pre><code>var http = require('http');
  var url = require('url');
  var database = require('./database');
  
  
  // Generic find methods (GET)
  
  function findAllResources(resourceName, req, res) {
    database.find('OrderBase', resourceName, {}, function (err, resources) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resources));
    });
  };
  
  var findResourceById = function (resourceName, id, req, res) {
    database.find('OrderBase', resourceName, {'_id': id}, function (err, resource) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resource));
    });
  };
  
  // Product methods
  
  var findAllProducts = function (req, res) {
    findAllResources('Products', req, res);
  };
  
  var findProductById = function (id, req, res) {
    findResourceById('Products', id, req, res);
  };
  
  // Generic insert/update methods (POST, PUT)
  
  var insertResource = function (resourceName, resource, req, res) {
    database.insert('OrderBase', resourceName, resource, function (err, resource) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(resource));
    });
  };
  
  // Product methods
  
  var insertProduct = function (product, req, res) {
    insertResource('OrderBase', 'Product', product, function (err, result) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(result));
    });
  };
  
  
  var server = http.createServer(function (req, res) {
  
    // Break down the incoming URL into its components
    var parsedURL = url.parse(req.url, true);
  
    // determine a response based on the URL
    switch (parsedURL.pathname) {
      case '/api/products':
      if (req.method === 'GET') {
        // Find and return the product with the given id
        if (parsedURL.query.id) {
          findProductById(id, req, res)
        }
        // There is no id specified, return all products
        else {
          findAllProducts(req, res);
        }
      }
      else if (req.method === 'POST') {
  
        //Extract the data stored in the POST body
        var body = '';
        req.on('data', function (dataChunk) {
          body += dataChunk;
        });
        req.on('end', function () {
          // Done pulling data from the POST body.
          // Turn it into JSON and proceed to store it in the database.
          var postJSON = JSON.parse(body);
          insertProduct(postJSON, req, res);
        });
      }
      break;
      default:
      res.end('You shall not pass!');
    }
  });
  server.listen(8080);
  
  console.log('Up and running, ready for action!');
  </code></pre>