Build your svg on the server using Swagger, Node, Express

Recently, I had the need to share an svg chart between a javascript (React) app, an android app, and an iOS app. One option would be to write code in all three application to generate the chart, but a better option is to push the generation of the chart to our Swagger Express Server.

The chart I created only had a couple dozen data points on it, but its easy to imagine a case were we had many hundreds of points that we would want to use the server to generate. Moving your svg to the server also gives you the ability to use server caching, and insulate you from the limitations of your user’s machine.

Swagger yaml

First we need to setup swagger to return our image

swagger: "2.0"
info:
  version: "0.0.1"
  title: SVG App
host: localhost:10010
basePath: /
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - image/svg+xml
paths:
  /svg:
    x-swagger-router-controller: svg
    get:
      operationId: chart
      summary: Get the logo image
      responses:
        "200":
          description: Success
          schema:
            type: file
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
definitions:
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

Controller

Next we need to setup our controller to return the correct content-type and call our service to build the chart.

'use strict';
 
const chartService = require('../services/chartService')
 
module.exports = {
  chart: chart
}
 
function chart (req, res) {
 
  res.setHeader('content-type', 'image/svg+xml')
 
  res.status(200).send(chartService.buildChart())
 
}

Service

Finally, let’s build our svg in our ChartService. In this simple example, its just a red circle, but here you could use a library like d3-node to create something more powerful.

Build svg in service

'use strict'
 
module.exports = {
  buildChart: buildChart
}
 
function buildChart(){
 
  const chart = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />Sorry, your browser does not support inline SVG. </svg>'
 
  return chart
 
}

Check out the whole example project.

About the Author

Scott Bock profile.

Scott Bock

Principal Technologist

Scott is a Senior Software Engineer with over 12 years of experience using Java, and 5 years experience in technical leadership positions. His strengths include troubleshooting and problem solving abilities, excellent repertoire with customers and management, and verbal and written communication. He develops code across the entire technology stack including database, application, and user interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]