lmarkus / Kraken_Example_Shopping_Cart

An end-to-end example of a shopping cart built with Kraken
92 stars 47 forks source link

method-override not working for Delete Solved? #18

Open quarterpi opened 8 years ago

quarterpi commented 8 years ago

I am having issues with method-override. I'm not sure where how to server.use it because version 2.2.2 doesn't have an app.requestBeforeRoute() function. Where are the hooks in v2.2.2? I also tried putting it in the options.onconfig() function like this... (the code inside the methodOverride function came from one of method-override's examples here. https://www.npmjs.com/package/method-override )

options = {
    onconfig: function (config, next) {
        /*
         * Add any additional config setup or overrides here. `config` is an initialized
         * `confit` (https://github.com/krakenjs/confit/) configuration object.
         */

        app.use(methodOverride(function(req, res) {
            debugger;
            if (req.body && typeof req.body === 'object' && '_method' in req.body) {
                var method = req.body._method;
                return method;
            }
        }));
        next(null, config);
    }
};

Obviously, this didn't work. What am I missing?

quarterpi commented 8 years ago

Okay, I feel kind of foolish. Here is the code that is working for me... Please let me know if this is the proper way to do this is Kraken v2.2.2. I am getting the results expected with no errors. In index.js ...

'use strict';

var express = require('express');
var kraken = require('kraken-js');
var db = require('./lib/database.js');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

var options, app;

/*
 * Create and configure application. Also exports application instance for use by tests.
 * See https://github.com/krakenjs/kraken-js#options for additional configuration options.
 */
options = {
    onconfig: function (config, next) {
        /*
         * Add any additional config setup or overrides here. `config` is an initialized
         * `confit` (https://github.com/krakenjs/confit/) configuration object.
         */

    db.config(config.get('databaseConfig'));
        next(null, config);
    }
};

app = module.exports = express();
app.use(kraken(options));

// This is what what missing before. I didn't realize that kraken wasn't already app.using bodyPaser. (I'm actually still not sure)
app.use(bodyParser.urlencoded({extended: false}));
// add in faux http method support (not sure if this is where this should go
app.use(methodOverride(function(req, res) {
    if (req.body && typeof req.body === 'object' && '_method' in req.body) {
        var method = req.body._method;
        return method;
    }
}));

app.on('start', function () {
    console.log('Application ready to serve requests.');
    console.log('Environment: %s', app.kraken.get('env:env'));
});

I hope this helps someone with the same issue that I was having. ;)