Last active
March 31, 2018 16:57
-
-
Save prabirshrestha/10028584 to your computer and use it in GitHub Desktop.
Server side reactjs rendering in Nancy with React.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bootstrapper : DefaultNancyBootstrapper | |
{ | |
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) | |
{ | |
base.RequestStartup(container, pipelines, context); | |
var react = new ReactEnvironment( | |
new JavaScriptEngineFactory(), | |
new ReactSiteConfiguration(), | |
new NullReactCache(), | |
new NancyReactFileSystem(container.Resolve<IRootPathProvider>())); | |
container.Register<IReactEnvironment>(react); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
// views/hello.jsx | |
var HelloWorld = React.createClass({ | |
render: function () { | |
return ( | |
<div>Hello {this.props.firstName} {this.props.lastName}.</div> | |
); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HelloWorldModule : NancyModule | |
{ | |
public HelloWorldModule(IReactEnvironment react) | |
{ | |
Get["/"] = _ => { | |
var hellojs = react.LoadJsxFile("~/views/hello.jsx"); | |
react.Execute(hellojs); | |
var component = react.CreateComponent("HelloWorld", new Person { firstName = "Prabir", lastName = "Shrestha"}); | |
/// var component = react.CreateComponent("HelloWorld", new { firstName = "Prabir", lastName = "Shrestha" }); | |
var html = component.RenderHtml(); | |
return html; | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NancyReactFileSystem : IFileSystem | |
{ | |
private readonly IRootPathProvider _rootPathProvider; | |
public NancyReactFileSystem(IRootPathProvider rootPathProvider) | |
{ | |
_rootPathProvider = rootPathProvider; | |
} | |
public string MapPath(string relativePath) | |
{ | |
if (relativePath.StartsWith("~/")) | |
{ | |
return Path.Combine(_rootPathProvider.GetRootPath(), relativePath.Substring(2)); | |
} | |
return relativePath; | |
} | |
public string ReadAsString(string relativePath) | |
{ | |
return File.ReadAllText(MapPath(relativePath)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NullReactCache : ICache | |
{ | |
public T GetOrInsert<T>(string key, TimeSpan slidingExpiration, Func<T> getData, IEnumerable<string> cacheDependencyFiles = null, | |
IEnumerable<string> cacheDependencyKeys = null) | |
{ | |
return getData(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public string firstName { get; set; } | |
public string lastName { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and plz let me know what github repository the above code is using?