Javascript - A Beginner Guide

Here’s a good guide to get you started on Javascript in a practical way. Why another guide? Practical - don’t learn for the sake of learning, learn for the sake of doing! Tactical - learn what matters, fast (yes, that’s what everyone says) Strategic - see the big picture This guide will get you started from the beginning, graduate you to using industry standard tools, and point you towards the right direction for learning even more than what can be offered in one guide or course. ...

Convert XML to JSON in Node

Here’s a quick demo of how you can convert XML to JSON in NodeJS. Problem You are given a slew of XML files and you have to convert them to JSON. But why JSON? It takes lesser space Faster and easier to work with What are we dealing with here? Convert this input - <?xml version="1.0" encoding="UTF-8"?> <root> <string name="msg">Message</string> <string name="hello">Hello</string> </root> .. to something that we can relate to in JSON - ...

Starter Template for AdonisJS

I have used AdonisJS in and out of projects in the last year. Despite it’s smaller community and not-so-regular enhancements, it remains one of my favourite NodeJS frameworks. I recommend AdonisJS for hobby and production projects in full confidence :) I typically use AdonisJS for back-end, which has me start with - adonis new awesomeness --api AdonisJS comes with a neat set of powerful features - An ORM that works quite well Authentication and security already setup Basic features to serve API without pulling a thousand wires just to get started The Case for a New Boilerplate Even with all the features baked in the default boilerplate, I tend to just copy older projects - ...

Fastode Server Framework

Fastode is an experimental boilerplate / framework based on Fastify. Why another boilerplate? Well, I just wanted to experiment with technologies to create MVP products. Although I have used NestJS, AdonisJS and friends before, those frameworks, while being great, bring their structure and overhead to any project. I wanted to check how difficult or easy it is to just start with a bunch of basic features and leave everything else to the project. ...

Use Packages Locally Without Publishing Them

How can you use packages being developed locally in other projects - without publishing them to npm repository? NPM Packages Make Life Exciting NPM makes it really easy to create packages and reuse them across projects. I am not even talking about big, useful libraries like Vue, Loadash etc. - just the routine problems across our projects. For example - I use a module to validate a bunch of things and start user subscription for an app I use similar starter theme for sites across projects It is easier to create NPM packages and reuse them instead of copying the code everywhere and starting the great struggle for its maintenance. ...

Refer Project Folder from NPM Package

You can refer to other files/folders using a simple require or using basic node libraries. But, how can you do that with npm packages? The Problem When creating a bunch of Javascript files, here’s what you can do to refer other files - const awe = require("../plugins/awesome.js"); This fetches awesome.js from the plugins folder in the parent. You can do a more robust solution with - const awe = require(path.join(__dirname, "../", "plugins")); __dirname fetches the current directory, and you just join that with the path to reach to the file or folder. ...

Fastify and its plugins are great

I had been meaning to use more of Fastify for work, but only made that a reality recently. So far I have had a great experience on the server framework. What is Fastify? Fastify is a low-overhead server framework built on top of NodeJS. Fastify was started in late 2016 and jumped to v1.0 in Q1,2018. So - it’s a comparatively younger framework. But that has not stopped it from having great ambitions. ...

npm run does nothing at Svelte startup

I was getting back to Svelte for a small project and ran into a strange issue. npm run dev did not run the svelte app as expected - no output or errors. First I create a new project. npx degit sveltejs/template awesome-project-1 cd awesome-project-1 npm i npm run dev And, I get greeted with nothing. I did not have any errors or output to debug. tldr; Set config ignore-scripts to false and retry. ...

Asserts are not just for testing

Assert is a general-purpose validator and should not be used in the context of testing alone. An assert in Javascript simply checks for truthy values and produces an error otherwise. We use it quite a bit in automated testing when checking for correctness of output for pre-defined inputs. We use asserts like so - var assert = require("assert"); function getSum(x, y) { return x + y; } assert(getSum(1, 2) === 3); assert(getSum(3, 6) == 9); The above code block will go through fine since there are no errors, but I am well capable of producing a few errors. ...

unknown vs. any in Typescript

Why use unknown when we have any in Typescript? Type inferences are powerful and allows us to do magic when we don’t know types. const i: any = 1; console.log(i); // 1 unknown seems to do the same thing. let x: unknown = 1; console.log(x); So, which to use where? Let us extend the previous examples to put that question to rest. Consider what happens when you try to do any operation on the type. const i: any = 1; console.log(i + 1); //2 const x: unknown = 1; console.log(x + 1); // error TS2365: Operator '+' cannot be applied to types 'unknown' and '1' unknown applies itself enthusiastically to equality operations, asserts and checks, but not for any other operations. ...