1

my RESTful services can't work after setting up my coding. Hardware I'm using is Raspberry PI. I created a folder called nodecellar which contains all my codes inside.

This is my server.js:

var express = require('express'),
    wine = require('./routes/wines');

var app = express();

app.configure(function () {
    app.use(express.logger('dev'));     /* 'default', 'short', 'tiny', 'dev' */
    app.use(express.bodyParser());
});


app.get('/wines', wine.findAll);
app.get('/wines/:id', wine.findById);
app.post('/wines', wine.addWine);
app.put('/wines/:id', wine.updateWine);
app.delete('/wines/:id', wine.deleteWine);

app.listen(3000);
console.log('Listening on port 3000...');

This is my package.json:

{
   "name": "wine-cellar",
   "description": "Wine Cellar Application",
   "version": "0.0.1",
   "private": true,
   "dependencies": {
       "express": "3.x"
   }
}

This my wines.js in subfolder:

var mongo = require('mongodb');
var Server = mongo.Server, Db = mongo.Db, BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('winedb', server);

db.open(function(err, db) {
   if(!err) {
      console.log("Connected to 'winedb' database");
      db.collection('wines', {strict:true}, function(err, collection) {
          if (err) {
              console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
            populateDB();
          }
      });
    }
});

exports.findById = function(req, res) {
    var id = req.params.id;
    console.log('Retrieving wine: ' + id);
    db.collection('wines', function(err, collection) {
        collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
            res.send(item);
        });
    });
};

exports.findAll = function(req, res) {
    db.collection('wines', function(err, collection) {
        collection.find().toArray(function(err, items) {
            res.send(items);
        });
    });
};

exports.addWine = function(req, res) {
    var wine = req.body;
    console.log('Adding wine: ' + JSON.stringify(wine));
    db.collection('wines', function(err, collection) {
        collection.insert(wine, {safe:true}, function(err, result) {
            if (err) {
                   res.send({'error':'An error has occurred'});
            } else {
                   console.log('Success: ' + JSON.stringify(result[0]));
                   res.send(result[0]);
            }
        });
    });
}

exports.updateWine = function(req, res) {
    var id = req.params.id;
    var wine = req.body;
    console.log('Updating wine: ' + id);
    console.log(JSON.stringify(wine));
    db.collection('wines', function(err, collection) {
         collection.update({'_id':new BSON.ObjectID(id)}, wine, {safe:true}, function(err, result) {
            if (err) {
                   console.log('Error updating wine: ' + err);
                   res.send({'error':'An error has occurred'});
            } else {
                   console.log('' + result + ' document(s) updated');
                   res.send(wine);
            }
         });
    });
}

exports.deleteWine = function(req, res) {
    var id = req.params.id;
    console.log('Deleting wine: ' + id);
    db.collection('wines', function(err, collection) {
         collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
             if (err) {
                        res.send({'error':'An error has occurred - ' + err});
             } else {
                        console.log('' + result + ' document(s) deleted');
                        res.send(req.body);
             }
         });
     });
}

var populateDB = function() {

    var wines = [{
              name: "CHATEAU DE SAINT COSME",
              year: "2009",
              grapes: "Grenache / Syrah",
              country: "France",
              region: "Southern Rhone",
              description: "The aromas of fruit and spice...",
              picture: "saint_cosme.jpg"
     },
     {
              name: "LAN RIOJA CRIANZA",
              year: "2006",
              grapes: "Tempranillo",
              country: "Spain",
              region: "Rioja",
              description: "A resurgence of interest in boutique vineyards...",
              picture: "lan_rioja.jpg"
     }];

     db.collection('wines', function(err, collection) {
          collection.insert(wines, {safe:true}, function(err, result) {});
          });

};
2
  • what error? Do you get it executing node or when a client hits the service? How are you hitting the service? What does hitting the service with curl -v ... tell you?
    – rob
    Commented Jul 10, 2014 at 7:50
  • It stated that TypeError: Object function (req, res, next) { app.handle(req, res, next);} has no method 'configure', rob Commented Jul 10, 2014 at 8:05

1 Answer 1

1

There seems to be some breaking changes in the versions 3.x and 4.x of express.

3.x used the

app.configure method

4.x now uses

app.set

A number of user have been hit by the same error message

https://stackoverflow.com/questions/22202232/express-has-no-method-configure-error https://stackoverflow.com/questions/22265143/typeerror-object-function-req-res-next-app-handlereq-res-next-has

3
  • It displayed Cannot GET when starting up my RESTful services, rob Commented Jul 11, 2014 at 8:26
  • 1
    can you update your question with the exact output?
    – rob
    Commented Jul 11, 2014 at 9:43
  • I can't get the exact output, rob Commented Jul 11, 2014 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.