Ishanka Ranatunga

Software Engineer | Tech Geek

How to use Typescript with NodeJS + Express

If you have worked with Angular projects, I’m sure that you are familiar with Typescript. But have you ever thought using it in your NodeJs backend?

Typescript, we can simply say that it is a superset of Javascript which brings optional static type-checking. Also Javascript programs are valid Typescript programs.

I used to code Nodejs backends with Javascript. But when i get hang of Typescript, I realized how easy it is. When you code with Typescript, it is really easy to catch the issues before executing the code. Since the syntaxes are similar to C#, Java it’s really easy to learn if you are familiar with those languages.

Assuming you are already familiar with NodeJS, time to get back into our topic. If you know NodeJs, you probably should have heard about the most popular web framework, ExpressJS.

To get started, initialize a npm project in a selected directory, using npm init.

Now you have to install dependencies.

We need to install Typescript as dev dependency.

npm i -D typescript

Great, now we can install our framework by using following commands.

npm i express

npm i -D @types/express @types/node

The second command installs typings for ExpressJS. Since ExpressJS and Typescript are independent packages, there is no way for Typescript to know about the types of the ExpressJS classes.

Installing those package is not enough, we need to configure our project for Typescript. For that we need to have a file called tsconfig.json. You can simply create that file by using the following command on your directory.

tsc --init

That file will have a list of properties which you can configure, and also those properties are described there in the comments. If you want to know more about those key-value options, you can find all the details here. In our case, we just need the following configuration.

Now we will create a src folder in the root directory which is where our Typescript files are going to be. Inside of the directory lets create a file, app.ts. Now our project structure should look like this;

Now let’s create our express server. To do that paste the following code into the app.ts file we created earlier.

That is a very basic express server which simply returns ‘Hello World!’ when you make a GET request to http://localhost:3000.

To start the server you just have to execute the following command.

tsc && node dist/app.js

This command will compile Typescript code into Javascript and the compiled ones will be in the dist folder which is the output directory we configured earlier in the tsconfig.json file. From there we are going to start the node server.

If it runs successfully, we get a message on the console saying ‘Server is running…’. Now you can make the GET request. For that you can simply visit the http://localhost:3000 on the browser and you will see the message.

You can add the above command as the npm start command, so that next time you can just do a npm start.

Alternatively, you can install ts-node which is something similar to nodemon. With that you don’t need to run command every time when you make a change in the code. ts-node will automatically detect the changes, compiled it and restart the server.

If you are using ts-node, just run ts-node src/app.ts.

Great! Now you have working Nodejs project with Typescript.

A quick guide to Serverless Framework with AWS Lambda

In my previous article, I talked about what Serverless is. In case you missed it, I suggest you to read it in here. 😄

So in this article I’m going to tell how to create a simple serverless function with AWS Lambda in a simple way. 😉 If you are familiar with AWS Lambda Console, you know the pain of creating and managing the Lambda function. Fortunately there are now so many frameworks that we can use to reduce our workload. When you start to use a framework you will know how simple it is.

Let me list down some of the framework that we can use.

From all of the above, I have been using the Serverless Framework for like a year to deploy my Lambdas. I found it’s really easy to use and configure. Also, there are lot a plugins for that framework by its’ great community. 🙇‍♂️

ezgif-6-c9b3ee086989.gif

If you want to use another provider instead of AWS, it’s really easy to migrate. You just need to modify few lines and you are good to go. It’s that simple. 😉

So, in this article I’m going to show you how to create and deploy a simple NodeJs Lambda function with Serverless Framework. Let’s get started!

Firstly, we need to install the serverless framework using the following command.

npm install -g serverless

After installing the npm package, we can either use serverless or sls which are equivalent commands.

Now we need to configure the AWS IAM User credentials for Serverless Framewrok. To do that run the following command. If you don’t know how to get those, you can refer this guide.

serverless config credentials --provider aws --key AWS_ACCESS_KEY --secret AWS_SECRET

To create a serverless project, they have given us boilerplates for each language and the providers they support. You can find all of them here. In this case we are going to initiate the project with the following command for NodeJs.

serverless create --template aws-nodejs --path simple-lambda

Now we have two files named handler.js and serverless.yml inside the directory. The handler.js file basically contains our lambda function code. Here we can add more functions as much as we need for our project.

The serverless.yml is where the magic happens. This file contains the configuration of our project where it defines the details of the service provider, environment variables,  functions, resources, serverless plugins etc.

For every function that you write in the handler.js file, you need to configure here. Basically that configurations are responsible for creating API Gateway routes for the functions.

So what if we want to test the code that we wrote is working? For that we can use the following command;

serverless invoke --function hello --log

This command will run our hello function and give us the logs.

Okay, then what about if we need a local API for all the functions?

This is where a plugin comes handy. We can use a plugin called serverless-offline which will help us to run a local API for our project. Installing a plugin is simple. You just need to install the npm package and add the plugin name to the serverless.yml.

npm install serverless-offline

After adding the plugin to the serverless.yml, it should look like this;

Now you can run the following command to create the local API for your project.

serverless offline start

Response:
Serverless: Starting Offline: dev/us-east-1.
Serverless: Routes for hello:
Serverless: GET /
Serverless: POST /{apiVersion}/functions/simple-lambda-dev-hello/invocations

Serverless: Offline [HTTP] listening on http://localhost:3000
Serverless: Enter "rp" to replay the last request

Now you can make a GET request to http://localhost:3000/ and see the response.

Once we are done with testings, we can use the following command to deploy the our code to AWS.

serverless deploy

Response:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service simple-lambda.zip file to S3 (386 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...........................
Serverless: Stack update finished...
Service Information
service: simple-lambda
stage: dev
region: us-east-1
stack: simple-lambda-dev
resources: 9
api keys:
  None
endpoints:
  GET - https://oujlmpmbt1.execute-api.us-east-1.amazonaws.com/dev
functions:
  hello: simple-lambda-dev-hello
layers:
  None

This will deploy all the functions, resources, events that you defined in the serverless.yml.

Yey! Now you have an endpoint for your deployed Lambda function.

tenor

If you want to see the logs of your deployed Lambda function you can use;

serverless logs --function hello

Response:
START RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e Version: $LATEST
END RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e
REPORT RequestId: 7ffe81b5-3221-46cd-aaab-61ce7921148e  Duration: 2.46 ms       Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 59 MB  Init Duration: 1.68 ms

Visit the official docs if you want to find out more about the logs.

Okay, what if you want to remove the functions which you deployed? Again, it’s just one simple command.

serverless remove

Response:
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
..........
Serverless: Stack removal finished...

Look how simple was that. Now you can really focus on your code and make your life easier with AWS Lambda.

That’s all folks. Happy Coding! ☺️

A Short Introduction to Serverless & FaaS

Hey folks 👋, it has been a while since my last post 👉 An Introduction to NoSQL.  Recently, I had to revise some of the things that I have learned about in past few months. One of them is the Serverless Architecture. So I decided to write a post about it while I’m revising. 😁

Serverless?

Serverless is a hot 🔥 trend in cloud computing. The serverless architecture is not actually “serverless” though. .
tenorYes. It is just the name. 😉 Serverless does not mean that there are no servers involved. It simply means, the developers don’t have to think about server management and low-level infrastructure. The cloud provider will be responsible for managing resources, provisioning, patching and make servers available for the tasks. So that the developer can focus only in coding. 👨‍💻1_dAnZk19kGszKTvAgag31sQ.jpeg

In Serverless Architecture, applications significantly depend on third-party services known as Backend-as-a-Service (BaaS) or on custom code that’s run in ephemeral containers  known as Function-as-a-Service (FaaS).

  • BaaS :
    Applications that depend on 3rd party applications/services to execute a certain amount of the application’s logic and state.
    eg: Auth0, Firebase
  • FaaS :
    Applications that run in stateless compute containers that can be triggered via events.
    eg: AWS Lambda, Google Cloud Functions

Basically, FaaS is about running back-end code without managing your own server. Here instead of writing applications, we can write piece of applications with event rules which trigger our code when needed. So that you’ll be billed only for the fraction of a second, measuring resource usage. 💰

I have worked with PaaS solutions like, Heroku. As the PaaS providers promised they have offered us the freedom of managing servers but they don’t have great auto scaling, so we have to still think about it. 🤔 Also PaaS is designed for long-running applications. So that the application is running always to serve the incoming requests. In FaaS, our function begins to serve a request and terminated after it is processed. That means, when there are no requests, our functions should not consume the resources.

But there is a issue, ☝ not all workload can be converted to a event triggered model and neither can all code be separated by it’s dependencies and some of them may require intensive installation and configurations. So we should keep in mind that FaaS cannot be used in every situations.

Here are some use cases of FaaS; 👍
-Backends
-Web Apps
-Scheduled Tasks
-Chat Bots
-IoT Services

Let’s list down some benefits of Serverless approach 👇

  • In serverless, you are only charged for the time it took the function to run. So we don’t have to pay for the idle time.
  • Serverless gives auto scaling without involving us. Depending on the load cloud provider will scale up function instances.
  • We can use different language runtime according to the use case
  • Developers can focus on coding without worrying about the server management

There are drawbacks also. Below are some of them 👇

  • Serverless can also add complexity rather than reducing it.
  • Potentially hard to debug
  • Hard to work with different services : using Firbase instead of DynamoDB
  • Cold start : It takes some time for a scalable serverless platform to handle the first request by a function

Serverless architecture allows us to write piece of applications with event rules which trigger our code when needed and run quickly without using lot of server resources. But it doesn’t mean that FaaS is usable only in small scenarios. Although a function is a small unit, it can be invoked millions of times per second. The question is what functionalities we should move from our application to functions and how to do it without making our application complex. 💭

 

An Introduction to NoSQL

Hi there! With the new look of my blog, I decided to talk about a different subject. Let’s jump straight into the today’s subject without any small talks. 😛

In the world of computer systems, there are large number of data comes out every day. A significant amount of those data is handled by Rational Database Management Systems (RDBMS) which is a predominant technology for storing structured data in computer systems.

There are different types of data;

  • Structured Data : Information with the high degree of organization and readily searchable by simple,straight forward search operations.  
  • Unstructured Data : Information that is not organized in a pre-defined manner neither a pre-defined model.
  • Semi-Structured Data : A cross between above two. Type of structured data. but lacks the strict data model structure.
RelationalDataLake2_thumb_4641DE12

Structured, Semi-structured and Unstructured Data

As i said, the structured data is handled by RDBMS, what happens to the unstructured and semi-structured data then? What if your data requirements at the beginning are not clear and you are dealing with massive amounts of rapidly increasing unstructured data (time series data such as – IoT and device data, user and session data; chat, messaging, log data) ?

RDBMS are not designed to manage these type of data efficiently, and this is where NoSQL comes into the stage with the capability of handling huge amount of data properly.

What is NoSQL?

NoSQL means “Not only SQL” and it is an approach to database systems which is far away from traditional RDBMS. Unlike RDBMS, NoSQL satisfies the next-generation data-intensive application requirements which are performance, scalability and flexibility. NoSQL is very useful when it comes to storing unstructured data which is growing rapidly than structured data.

How NoSQL achieve those?

Instead of tables like in RDBMS. NoSQL is document-oriented. So the unstructured data is stored in a single document that can be easily found and it is not necessary to categorize into fields like in RDBMS.  Not only the unstructured data, it can handle structured and semi-structured data also.

Developers who are working with object-oriented programming languages such as Java, PHP, C# and Python use APIs such as JPA, Hibernate, LINQ which allow developers to execute queries without having to learn SQL. NoSQL is natively object-oriented, easy to use and flexible. So it sidestep by using APIs.

NoSQL provides horizontal scaling rather than vertical. It’s cost-effective when comparing with RDBMS which is vertically scalable by increasing server hardware power and bigger servers.

nosql-data-stores-in-research-and-practice-icde-2016-tutorial-extended-version-11-638

Vertical Scaling vs. Horizontal Scaling

 

Types of NoSQL Databases

  • Key-Value Stores :
    The most simple NoSQL option which is very useful in accelerating an application’s read-write speed and processing of non-transactional data. Stored values can be any type of binary object (text, video, JSON, etc.) and are accessed via a key.
    Eg: Redis, Riak, Azure, DynamoDB
    keyvaluepairs
  • Document Stores :
    This uses the Key-Value as concept but complex. A value is a single document that stores all data related to a specific key. It’s own a unique key, which is used to retrieve. Popular fields in the document can be indexed to provide fast retrieval of data without knowing the key. Each document has same or different structure.
    Eg: MongoDB, CouchDB i4
  • Wide Column Stores :
    Stores data as columns rather than rows and groups columns of related data together. A query can easily retrieve related data in a single operation because only the columns associated with the query are retrieved. In RDBMS, the data would be in different place on disk, requiring multiple operation for retrieval.
    Eg: BigTable, Cassandra, SimpleDB
    column-value-store
  • Graph Stores :
    Uses graph structures to store, map and query relationship. The adjacent elements are linked together without using an index. This method is the most complex one.
    Eg: Neo4J, OrientDB, Polyglot
    RequestInGraph

Why NoSQL?

Think of a situation where your server-side application is developed to be fast and seamless, but you use RDBMS to handle data which will bottleneck the data. If you use NoSQL, it will prevent from data being bottleneck.
Think about an AI or a machine learning model. What will happen if you use RDBMS over NoSQL?

When it comes to Big Data, NoSQL is the hero doing the things that traditional RDBMS cannot.

A NoSQL database has no limit when it comes to storing types of data. You can store them together and allows to add new different types as you need. With document based database you will store data in one place without having to define the types.

In cloud computing, NoSQL is the best cost-saving approach. Some NoSQL databases are increasingly begin adopted to high availability and they use master-less database architecture that automatically distribute data equally among multiple resources. By automatically replicating over multiple data centers, distributed NoSQL databases can ensure a great application experience wherever users are located by reducing the latency.  NoSQL databases like Cassandra are designed to be scaled across multiple data centers without any hassle.

BUT, there are some disadvantages also.
A major problem in NoSQL is lack of reporting tools. However, SQL have variety of reporting tools available.
The community is relevantly new when comparing with the community that SQL has. But NoSQL is rapidly growing.

Conclusion

NoSQL databases are becoming a major role in the database landscape. With the advantages like low-cost, performance, high availability and scalability, it is a great option for companies who integrate in big data. But, NoSQL is a still young technology and does not have standards that SQL databases like MySQL has. At the end of the day, the choice between SQL and NoSQL depends on the complexity, variety and volume of data that you are going to work with.