The coordinates for the middle of the line are the averages of the coordinates of the ends:
var midX = (x1 + x2) / 2;
var midY = (y1 + y2) / 2;
The directional vector of the perpendicular is the same as the original line, except the coordinates are flipped and one of the coordinates is multiplied by -1 (which one you multiply decides whether the line will be clockwise or anticlockwise from the original). Add that directional vector to the midpoint and you have your perpendicular.
var dx = (x2 - x1);
var dy = (y2 - y1);
context.moveTo(midX, midY);
context.lineTo(midX + dy, midY - dx);
context.stroke();
This will draw a perpendicular that is the same length as your original line. If you want to draw a different length you'll need to adjust the directional vector. Read up on coordinate geometry and vectors for more details.