Full Stack Development From Client-Side JavaScript [No Node.js]
We take a look at one developer's experiment, where he hopes to create a way to code a full stack app using only client-side JS.
Join the DZone community and get the full member experience.
Join For FreeIn case you think the above header sounds like an oxymoron or some sort of black magic, think again. With Lizzie, you can (at least in theory) create your entire business logic in JavaScript, in your browser, submit your Lizzie code to the server for evaluation (securely), and have it return JSON back to your client.
For the record, this assumes you've previously created generic "hooks" in C#, allowing your Lizzie code to do something useful, since, out of the box, Lizzie can barely do anything at all, besides from creating lists, dictionaries, do basic math, and some simple string manipulation. However, if you create generic methods for retrieving data from your database, etc., creating your business logic in JavaScript as Lizzie code, and transmitting this code to your server for evaluation, is actually a breeze. This in turns creates a much more dynamic server-side API, allowing you to (after a while) do anything you want from JavaScript, without having to code in (any) server-side language at all. At least that's how the theory goes. To understand what I am talking about, picture the following code.
[Route("api/[controller]")]
public class LizzieController : Controller
{
[HttpPost]
public string Post()
{
try
{
using (var reader = new StreamReader(Request.Body))
{
string code = reader.ReadToEnd();
var lambda = LambdaCompiler.Compile(this, code);
var result = lambda();
return result?.ToString() ?? string.Empty;
}
}
catch (Exception err)
{
Response.StatusCode = 500;
return err.Message;
}
}
#region [ -- Lizzie functions -- ]
[Bind(Name = "howdy")]
object Howdy(Binder<LizzieController> binder, Arguments arguments)
{
return "Hello " + arguments.Get<string>(0);
}
#endregion
}
The beauty of the above C# Web API code is that the endpoint accepts CODE as input. This allows you to create a single HTTP REST endpoint, accepting code over a POST request, that will evaluate your code (securely), returning JSON back to the client.
Which again effectively allows you to (at least in theory) create your entire application in client-side JavaScript, connecting to a "generic" backend, reducing the server to a simple "Lizzie evaluator," making it possible to create arguably your entire web app in Angular, React, etc. Below is a screenshot of a simple example application I created with this pattern, that allows me to type code into a CodeMirror instance in my browser, send my code to my server, evaluate the code, and display the results of the evaluation in another CodeMirror instance.
Opinions expressed by DZone contributors are their own.
Comments