diff --git a/samples/misc/NodeServicesExamples/Controllers/HomeController.cs b/samples/misc/NodeServicesExamples/Controllers/HomeController.cs
index 34ce07c..7a4f804 100755
--- a/samples/misc/NodeServicesExamples/Controllers/HomeController.cs
+++ b/samples/misc/NodeServicesExamples/Controllers/HomeController.cs
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.NodeServices;
namespace NodeServicesExamples.Controllers
{
@@ -15,8 +16,21 @@ namespace NodeServicesExamples.Controllers
return View();
}
- public IActionResult ImageResizing()
+ public async Task Chart([FromServices] INodeServices nodeServices)
{
+ var options = new { width = 400, height = 200, showArea = true, showPoint = true, fullWidth = true };
+ var data = new
+ {
+ labels = new[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
+ series = new[] {
+ new[] { 1, 5, 2, 5, 4, 3 },
+ new[] { 2, 3, 4, 8, 1, 2 },
+ new[] { 5, 4, 3, 2, 1, 0 }
+ }
+ };
+
+ ViewData["ChartMarkup"] = await nodeServices.InvokeAsync("./Node/renderChart", "line", options, data);
+
return View();
}
diff --git a/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs b/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs
deleted file mode 100644
index 43f45df..0000000
--- a/samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using System;
-using System.IO;
-using System.Threading.Tasks;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.NodeServices;
-using Microsoft.AspNetCore.StaticFiles;
-
-namespace NodeServicesExamples.Controllers
-{
- public class ResizeImageController : Controller
- {
- private const int MaxDimension = 1000;
- private static string[] AllowedMimeTypes = new[] { "image/jpeg", "image/png", "image/gif" };
-
- private IHostingEnvironment _environment;
- private INodeServices _nodeServices;
-
- public ResizeImageController(IHostingEnvironment environment, INodeServices nodeServices)
- {
- _environment = environment;
- _nodeServices = nodeServices;
- }
-
- [Route("resize/{*imagePath}")]
- public async Task Index(string imagePath, int maxWidth, int maxHeight)
- {
- // Validate incoming params
- if (maxWidth < 0 || maxHeight < 0 || maxWidth > MaxDimension || maxHeight > MaxDimension
- || (maxWidth + maxHeight) == 0)
- {
- return BadRequest("Invalid dimensions");
- }
-
- var mimeType = GetContentType(imagePath);
- if (Array.IndexOf(AllowedMimeTypes, mimeType) < 0)
- {
- return BadRequest("Disallowed image format");
- }
-
- // Locate source image on disk
- var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath);
- if (!fileInfo.Exists)
- {
- return NotFound();
- }
-
- // Invoke Node and pipe the result to the response
- var imageStream = await _nodeServices.InvokeAsync(
- "./Node/resizeImage",
- fileInfo.PhysicalPath,
- mimeType,
- maxWidth,
- maxHeight);
- return File(imageStream, mimeType);
- }
-
- private string GetContentType(string path)
- {
- string result;
- return new FileExtensionContentTypeProvider().TryGetContentType(path, out result) ? result : null;
- }
- }
-}
diff --git a/samples/misc/NodeServicesExamples/Node/renderChart.js b/samples/misc/NodeServicesExamples/Node/renderChart.js
new file mode 100644
index 0000000..370df76
--- /dev/null
+++ b/samples/misc/NodeServicesExamples/Node/renderChart.js
@@ -0,0 +1,8 @@
+var generate = require('node-chartist');
+
+module.exports = function (callback, type, options, data) {
+ generate(type, options, data).then(
+ result => callback(null, result), // Success case
+ error => callback(error) // Error case
+ );
+};
diff --git a/samples/misc/NodeServicesExamples/Node/resizeImage.js b/samples/misc/NodeServicesExamples/Node/resizeImage.js
deleted file mode 100644
index 207b149..0000000
--- a/samples/misc/NodeServicesExamples/Node/resizeImage.js
+++ /dev/null
@@ -1,8 +0,0 @@
-var sharp = require('sharp');
-
-module.exports = function(result, physicalPath, mimeType, maxWidth, maxHeight) {
- // Invoke the 'sharp' NPM module, and have it pipe the resulting image data back to .NET
- sharp(physicalPath)
- .resize(maxWidth || null, maxHeight || null)
- .pipe(result.stream);
-}
diff --git a/samples/misc/NodeServicesExamples/Views/Home/Chart.cshtml b/samples/misc/NodeServicesExamples/Views/Home/Chart.cshtml
new file mode 100644
index 0000000..2775261
--- /dev/null
+++ b/samples/misc/NodeServicesExamples/Views/Home/Chart.cshtml
@@ -0,0 +1,12 @@
+
Server-rendered chart
+
+
+ This sample demonstrates how arbitrary NPM modules can be invoked from .NET code.
+
+
+ In this case, we use node-chartist to render the following chart on the server. The output is
+ identical to what you'd get if you used chartist.js
+ on the client, except that in this example, we're not executing any client-side code at all.
+
- This sample shows how the NPM module sharp
- can be used for dynamic image resizing from within an ASP.NET Core application. There is one copy of the
- following image on disk, but we can set up an MVC action method that returns it resized to fit within an
- arbitrary width and height.
-
-
-
- Dependencies: On Windows and Linux, there are no native dependencies. Just running
- npm install is enough. On OS X, however, you need to have libvips installed,
- which you can get through Homebrew by running
- brew install homebrew/science/vips.
-