Search code examples
gruntjsyeoman

yeoman 1.0 - make development server accept POST calls


I'm using yeoman for my application which consists of 2 parts - client site with js/html/css and the rest service.

During development I start rest service in Eclipse and start server for my static files with

grunt server

The problem is that I have to do a post request to root url '/' (it's a fake login POST request to make browsers prompt to save passwords).

It worked with yeoman 0.9 but after updating I get:

Cannot POST /

Is there a way to configure grunt server task to accept POST requests?


Solution

  • I think you want the connect-rest middleware.

    https://github.com/imrefazekas/connect-rest npm install connect-rest --save-dev

    Edit Gruntfile.js, at the top

    var restSupport = require('connect-rest');
       restSupport.post( { path: '/savequestion'}, function(req, content, next){
       next(null, {result: 'OK'});
    });
    

    In your connect or livereload middleware section:

            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            lrSnippet,
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, yeomanConfig.app),
                            restSupport.rester( {'context': '/forms'} ),
                            rewriteRulesSnippet, // RewriteRules support
    

    The key part is "restSupport.rester()", remove the context if you don't want it.

    This simple function should just reply with the json object {result: 'OK'} to everything you post to /forms/savequestion . It should at least let you build out scaffolding in grunt server :9000 mode before you have build your templates. Without this you would have to $.get() each $.post() and then change it during or after the build.