Nov 30, 2017

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
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]