How To Post Data With Fetch API To An ExpressJS API Endpoint

Let’s start by assuming that our app is set up and express is running.

We need to have a route to

app.post("/capture", homeController.getPostRequests);

Let’s define our controller under src/controllers/homeController.ts:

import { Request, Response } from "express";

export let getPostRequests = (req: Request, res: Response) => {
  console.log(req.body); // Data coming from the frontend
  res.json({ "message": "got it", other: [ 1, 2, 3 ]  });
};

And import it in server.ts:

import * as homeController from "./controllers/homeController";

In the frontend we’ll have:

fetch("https://localhost:3000/capture",
{
  method: "POST",
  headers: {
    'Accept': 'application/json, text/plain, */*',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ user: "mimo", "email": "hello@example.com", "more": [ "a", "b", "c" ] })
})
.then(res => res.json() )
.then(data => { console.log(data) }) // Logs data coming from server

How to create a SSL certificate for localhost development in NodeJS/Express/TypeScript

Generate the required SSH keys. In the process we’ll be asked for a few questions about the organisation etc.

openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
openssl rsa -in keytmp.pem -out key.pem

Now we have our certificates ready for use.

In our index.ts:

import * as https from "https";
import * as fs from "fs";
import * as express from "express";

// expressJS options
const httpsOptions = {
  key: fs.readFileSync("/Users/mimo/.openssl/key.pem"),
  cert: fs.readFileSync("/Users/mimo/.openssl//cert.pem"),
  requestCert: false,
  rejectUnauthorized: false
};

const server = https.createServer(httpsOptions, app).listen(app.get("port"), function() {
  console.log("server started at port 3000");
});

Setting up a serverless infrastructure with AWS. API Gateway + AWS Lambda + AWS SQS

The idea: to have a system where we don’t have to worry about the implementation specific details of a solution. We’re going call an API which triggers a script that adds items to be elaborated to a queue.

Creating an AWS API Gateway

Continue reading Setting up a serverless infrastructure with AWS. API Gateway + AWS Lambda + AWS SQS

Amazon API Gateway: how to deploy and stage a newly create API

Went through the trouble of creating a new Amazon API gateway and now you seem unable to find where the invoke URL is defined? If yes keep reading.
Even reading the docs it takes a long time if you are not familiar with the confusing Amazon UI.

Continue reading Amazon API Gateway: how to deploy and stage a newly create API

How to run a search and replace with regex group capture substitution in VIM

Problem: you have a very large string to search and replace.
You want to use a regex for that (probably it is also the only way).
You want to use VIM.

To search across the whole file in VIM you need:

:%s

Then you want to add a regex, and since you want a group you have two options: escape all the parenthesis which I don’t want or use the special VIM very magic mode: \v.

Now you search for the group on the main part of the regex and substitute in the second:

:%s/\v(\w)/\1HELLO/g

This way you can export them all. In my case, I had a very large file (SQL Dump) where I had to convert binary values to use utf8mb4, so I wanted to run this:

:%s/\v(X'[^,\)]*')/CONVERT(\1 using utf8mb4)/g

Seeding a DB for testing purposes with Laravel and cross-references

  • Create a custom factory to fill up the data correctly
  • Call the factory with the right parameters depending on the status from the seeder
  • Fix the problems that are going to show up because of data structure.

Continue reading Seeding a DB for testing purposes with Laravel and cross-references

How to display fontawesome icomoon and font icons on a Windows 10 with blocked untrusted fonts

Windows 10 has a mode to disable font-loading from external sources, such as emails when the font has to be fetched outside of the installed fonts. This feature is meant to prevent from attacks coming from untrusted fonts.

The icons in our UI are made with a font file. We used icomoon, but if Windows 10 is set up with the same set similar to projects like fontawesome. We use custom fonts with icomoon.

However, this brings problems with displaying our UI in Firefox and IE11.

How can we solve this problem? The Microsoft recommended way it is to either install the font in the system or whitelist the application that needs that font.

We thought of three ways, at least initially:

  • Install the font
  • Use an SVG file
  • Propose the customer to whitelist the browser to let them use our app. This is the last option as it would make the setting less secure.

Continue reading How to display fontawesome icomoon and font icons on a Windows 10 with blocked untrusted fonts