Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method-override not working for Delete Solved? #18

Open
quarterpi opened this issue Jan 25, 2016 · 1 comment
Open

method-override not working for Delete Solved? #18

quarterpi opened this issue Jan 25, 2016 · 1 comment

Comments

@quarterpi
Copy link

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
Copy link
Author

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. ;)

@quarterpi quarterpi changed the title method-override not working for Delete method-override not working for Delete Solved? Jan 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant