Question

I am brand new to NancyFX and currently enthralled by its low-ceremony approach to web application development. Throwing myself in at the deep-end, I also want to use CoffeeScript and investigate the benefits of Sass.

The Set-Up

To enable this combination I have created a new Empty Web Application using the VS2010 template (found in the Nancy Accessories project). I have then used the VS PackageManager to Nancify my application and add the SassAndCoffee support:

PM> Install-Package Nancy
PM> Install-Package Nancy.SassAndCoffee

So far so good. I then created an ~/Content/scripts folder and in there I have placed a file called home.coffee containing the following line of CoffeeScript.

alert "Hello Nancy!"

Now things start to get a bit fuzzy. I want to run this script on the client so I create an view called ~/Views/home.sshtml (and associated NancyModule with Get["/"] route - not shown). The view's html looks like this:

<head>
    <title>Hello Nancy</title>
    <script type="text/javascript" src="/content/scripts/home.js"></script>
</head>
<body>
    <p>Hello @Model.User</p>
</body>
</html>

The view works just fine but the link to the home.js file just returns a 404: Not Found.

I am hoping that somehow Nancy will magically work out that I need my CoffeeScript compiled to JavaScript when it looks for the referenced home.js file and finds the home.coffee instead. This didn't work - so much for inspired guesswork.

If I change the script tag above to point to the existing home.coffee instead then the file is found but processed as a normal JavaScript file giving errors concerning the lack of tiresome ceremony namely: "unexpected string"

The Question

Now you know my set-up and simple requirements, here then is my question:

How do I get CoffeeScript to 'just work' using the NancyFX framework?

Thank you

Update

Steven Robbins (below) has answered this question by pointing to the demo code. But just in case you don't want to pull MBs of source from GitHub, here are the lines required to get things going. First add a class called Bootstrapper.cs to your project. Now add the following code (it worked like a charm for me):

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        StaticConfiguration.DisableErrorTraces = false;
        Hooks.Enable(pipelines, new InMemoryCache(), container.Resolve<IRootPathProvider>());
    }
}
Was it helpful?

Solution

The SassAndCoffee project doesn't hook into the static content bit in Nancy, it (or something similar) may in the future, but at the moment it's just a separate pipeline hook.

If you take a look at the sample project on github:

https://github.com/NancyFx/Nancy.SassAndCoffee/tree/master/src/Nancy.SassAndCoffee.Demo

That should show you how to get it going.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top