Sign in

I like making things and solving problems. http://nikhilvijayan.com.
image courtesy Datadog

Pino is a very low overhead logger for Node (their documentation claims it is over 5 times faster than alternatives). Here’s how to create a re-usable logger using Pino, and use Pino Noir.

The repository above is a very light wrapper around Pino and uses Pino Noir to redact certain keys.

Here’s what our main index.js module looks like:

const pino = require('pino');
const noir = require('pino-noir');
const { redactionKeys } = require('./redaction/redaction-keys');const serializers = { err: pino.stdSerializers.err };module.exports = pino({
level: process.env.LOG_LEVEL || 'info',
serializers: noir(serializers, redactionKeys),
})

Quick notes about the code above:

  • The index module…


I have now been getting paid to write software for two years. Here are a few things I wish I’d known on day 1. Some of it might seem obvious. I’m writing this for anyone who might be starting a (new) career in tech or software engineering.

1. Understand the business problem you’re solving

If a ticket is asking you to create a module that updates a database field, it’s easy to take that ticket and run with it — start writing integration tests, planning how the feature will work.

I wish I’d stopped to understand what problem this was solving at a business level. Ask questions…


Image from: https://www.flaticon.com/free-icon/paint-bucket_103998

I found it a bit hard to find out how to mock AWS S3 while using Jest. So, I thought I’d document it here.

Useful Stack Overflow answers:

We’ll be mocking S3, and asserting that S3’s copyObject function gets called with the right parameters.

Obviously, this is a simplistic function with hard-coded values, but the test is valid for a function that maybe does some transformation to the destination key, or does some validation on the CopySource, etc.

In the mockS3Instance I am using .mockReturnThis() . This is used to allow for chaining it to another method (in…


If, like me, you’ve spent way too long wondering what GraphQL is because sentences like query language for your API makes no sense to you. This blog post is for you.

In a REST API world, you have various endpoints to get, put and delete resources, right?

Let’s say we have an app that manages a leaderboard and we have the following routes:

POST /score/:playerId
GET /score/:playerId
GET /leaderboard/:leaderboardId
PUT /leaderboard/:leaderboardId

Now, say we have 6 players that are part of a leaderboard, on initial load we want to load up the state of the leaderboard, which includes all player…


I’m writing this for all of you who’ve been searching for terms like:

  • How to test stdout in Node
  • How to spy on stdout in Node using Jest
  • How to stub stdout in Node

It took me a day, but thanks to my colleague James Ransome for suggesting the use of Node’s child_process for this, it was fairly easy in the end!

Note: If you’re in a hurry, I’ve put together a repo that you can clone here: https://github.com/nkhil/testing-stdout

I wanted to test Pino’s output to stdout

Note: I know this counts as testing implementation details of a dependency, but in this case I was working on a…


This is a quick gotcha if you keep getting BadRequest without a useful error message trying to connect another container to localstack S3.

Here’s part of the error I was getting:

  message: null,
code: 'BadRequest',
region: null,
time: 2020-12-02T13:41:48.630Z,
requestId: null,
extendedRequestId: undefined,
cfId: undefined,
statusCode: 400,
retryable: false,
retryDelay: 85.98033816170914
}"

If you’re getting this error, check if you’ve specified a DEFAULT_REGION in the docker-compose.yml file.

Here’s what my docker-compose.yml looked like:

# docker-compose.ymllocalstack:    
networks:
testing_net:
ipv4_address: 172.28.1.9
image: localstack/localstack:latest
environment:
- SERVICES=s3:4566
- DEFAULT_REGION=us-east-1 👈 THE CULPRIT! …


First copy the contents of this docker-compose.yml file listed in the localstack repository into a local directory (make sure the file name is not changed).

Run up the container with docker-compose up. This will get localstack running locally.

At the time of writing this post (Oct 2020), the S3 service is available on port 4566 (i.e. http://localhost:4566)

Install the aws / localstack wrapper

Install awscli-local from here as a wrapper around localstack. This will allow you to use awslocal in your command line, which is very close to the aws command usage.

Create a new bucket

awslocal s3api create-bucket --bucket business-time-nonprod --region eu-west-1# You don't need the region flag…


Every time you require a file in node, these are the 5 things that take place:

  • Resolve
  • Load
  • Wrap
  • Evaluate
  • Cache

Resolve

Node will attempt to map the string given to require() into a path on the file system. This path could be local to node, under nodule_modules folder, or node modules under a parent directory or any other path.

Load

Node will then load the contents of this file into memory

Wrap

Wraps the content of the file with an IIFE (I’ve discussed this IIFE in another blog post How require and module.exports in node works)

Evaluate

Node will then evaluate the file…


I’ve always wondered how you always have access to require and module.exports in any .js files running with node. I recently found something cool.

If you console.log arguments in a file, and run it with node. For eg:

// someFile.js
console.log(arguments);

We actually get arguments:

[Arguments] {
'0': {},
'1': [Function: require] {
resolve: [Function: resolve] { paths: [Function: paths] },
main: Module {
id: '.',
path: '/someFolder',
exports: {},
parent: null…


This is a quick tutorial on setting up a simple express service using swagger 3.0.

Here’s the Github repo if you want to jump straight into the code: https://github.com/nkhil/swagger-3-setup.

Note: If you’d like to know why creating API definitions might be a good idea, this is a good read.

The biggest advantage of Swagger 3.0 that I’ve discovered are:

  • Request and response validation out of the box
  • Operation handlers to replace routers

Request & response validation

Let’s say our service has a GET route called /healthcheck/ready that we call to determine the readiness of the service.

We don’t want just anyone calling this route…

Nikhil Vijayan

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store