How to use TypeScript with Node - Pusher Blog

June 09, 2017 0 Comments

How to use TypeScript with Node - Pusher Blog

 

 

This blog post was written under the Pusher Guest Writer program.

TypeScript is a free open-source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript and adds optional static typing and class-based object-oriented programming to the language. Today we are going to learn how to use TypeScript with Node.js.

What Will You Build?

We will be building a simple Hello world application, which shows the current date and time using TypeScript and Node.js.
Below is a picture of what you will build:

Why Use TypeScript?

While there are lots of reasons why you should use TypeScript, here are three which I think are convincing enough.

  • Optional typing: In JavaScript, a variable is defined thus:
 var me = "myname"

In the code above, we leave it to the interpreter to guess which data type the variable is. In TypeScript, we can declare the same variable as:

 var me: string = "my name"

This gives the option of declaring what data type we want, which also helps while debugging.

  • Interfaces: TypeScript allows you to define complex type definitions in the form of interfaces. This is helpful when you have a complex type that you want to use in your application, such as an object which contains other properties.

Let’s take a look at this example:

 interface Usercheck{ name: string; age: int; } class User implements Usercheck { constructor (public name: string, public age: int) { } } 

The User class obeys, and must have data-types and definitions to, the interface Usercheck because it implements Usercheck. This is a function that is usually appreciated more by developers who use static languages.

  • TypeScript makes code easier to read and understand: The key to TypeScript is that it’s a statically typed script. Programming languages can either be statically or dynamically typed; the difference is when type checking occurs. Static languages’ variables are type checked which helps make the code more readable. The availability of classes also makes the code look more readable. For example:
 //Typescript class Test { //field name:string; //constructor constructor(name:string) { this.name = name } //function display_name():void { console.log("name is : "+this.name) } } //javascript var Test = (function () { //constructor function Test(name) { this.name = name; } //function Test.prototype.display_name = function () { console.log("name is : " + this.name); }; return Test; }());

In the snippet above, notice that it is easier to read the TypeScript version of the code than the JavaScript Version. In the TypeScript version, we can immediately see that the public variable called name is of the string data-type. In the JavaScript version, we cannot immediately figure out the data-type of the variable. Also, in the JavaScript Version, we had to use the prototype function to declare our display_name function, while in TypeScript we did not have to do that.

Getting Started

To install TypeScript globally so you can call it when needed, run:

 npm install -g typescript

For verification of the installed TypeScript, use the tsc --v command:

 tsc --v //Version 2.1.6

Next, install gulp for the build process. To do that, run:

 npm install -g gulp

Now, create a folder that will hold the application. To do this, run npm init and follow the instructions below:

 //create a new directory mkdir type-node //change directory to the new folder cd type-node //run npm init npm init 

You should now have all the basic libraries to start configuring for use.

Configuring TypeScript

Given no arguments, tsc will first check tsconfig.json for instructions. When it finds the config, it uses those settings to build the project.

Create a new file called tsconfig.json in the root folder and add the following:

 { "compilerOptions": { "target": "es6", "module": "commonjs" }, "include": [ "**/*.ts" ], "exclude": [ "node_modules" ] }

This defines the three major sections, which include compilerOptions, include and exclude parameters:

  • In the compiler options, a target of es6 has been set. This means that the JavaScript engine target will be set to es6, and the module will be set to CommonJS.
  • In the include block, a path has been set that scans for all TypeScript files and compile them.
  • In the exclude block, node_modules is being defined for it. TypeScript will not scan the node_modules folder for any TypeScript file. If you create a dummy index.ts file in the root, and run tsc on the command line, we will see the compiled **index.js** file.

Configuring Gulp

Although we have installed Gulp globally, we still need to install Gulp in our local project as well as Gulp-TypeScript. To do that, we run:

 npm install gulp gulp-typescript typescript --save

Doing just this setup done might be enough, but you would have to go back to the terminal to run the tsc command every time a change occurs. Instead, you can use Gulp to automate the process and run the compilation each time you change the file. Here’s how.

Add a gulpfile.js to the root of the directory. This enables automatic compilation of the source files. Now, put the following contents into the gulpfile.js:

 const gulp = require('gulp'); const ts = require('gulp-typescript'); // pull in the project Typescript config const tsProject = ts.createProject('tsconfig.json'); //task to be run when the watcher detects changes gulp.task('scripts', () => { const tsResult = tsProject.src() .pipe(tsProject()); return tsResult.js.pipe(gulp.dest('')); }); //set up a watcher to watch over changes gulp.task('watch', ['scripts'], () => { gulp.watch('**/*.ts', ['scripts']); }); gulp.task('default', ['watch']);

This creates a Gulp task which fires once a watcher triggers. A Gulp watcher watches for changes in the ts files and then compiles to JavaScript.
Now, delete the index.js file the tsc command created earlier. If you run Gulp, you will notice that the index.js file is re-created and that Gulp is now watching for changes.

Now that the Gulp watcher is running on one terminal, move to another terminal and continue the configuration.

Creating The App.ts File

First, we need to install the Express library by running the following:

 npm install express --save

The next step is to create the app.ts file that holds the Express app. Create an app.ts file, and copy the following into it:

 import * as express from 'express'; // Creates and configures an ExpressJS web server. class App { // ref to Express instance public express: express.Application; //Run configuration methods on the Express instance. constructor() { this.express = express(); this.middleware(); this.routes(); } // Configure Express middleware. private middleware(): void { } // Configure API endpoints. private routes(): void { /* This is just to get up and running, and to make sure what we've got is * working so far. This function will change when we start to add more * API endpoints */ let router = express.Router(); // placeholder route handler router.get('/', (req, res, next) => { res.json({ message: 'Hello World!' }); }); this.express.use('/', router); } } export default new App().express;

This has imported everything from Express and created a TypeScript class called App. A public property called express was also declared. Additionally, in the constructor, we had assigned an instance of the Express library to a public property called express which we defined earlier.
Finally, the middleware and routes function are being called. The middleware function defines middleware like CORS, templating engine, etc, while the routes function registers the routes, which have been set.

Configuring Express

Next, we will create an Express entry point, which we will write in TypeScript.

Create a new file called main.ts and add the following to it:

 import * as http from 'http'; import App from './App'; const port = 3000; App.set('port', port); //create a server and pass our Express app to it. const server = http.createServer(App); server.listen(port); server.on('listening', onListening); //function to note that Express is listening function onListening(): void { console.log(`Listening on port `+port); }

This has imported the HTTP module from node. The App class which runs the application is being imported.
Get a port value from the environment or set the default port number to 3000. Create the HTTP server, and pass App to it (Express app). Set up some basic error handling and a terminal log to show when the app is ready and listening.

If you run node main.js in the terminal, you will get a message that says listening on port 3000
Go to your browser, and you should see something like this:

Creating the **npm test** command
Inside our package.json file, let’s replace the part that defines the script section with the following:

 "scripts": { "test": "gulp" }

Now, any time we run npm test our Gulp command will run.

Notice that when you run npm test, it runs the Gulp watch script, and errors like “*****can not find some modules*****” might pop up. This is because TypeScript needs some definitions before it can read raw node libraries.
To install TypeScript definitions, use the @types notation with the packages. That is, to install Express TypeScript definitions, you will run npm install @types/express, but all the packages have already been installed, so we will install only the node types definition.
Open up the terminal and do this:

 npm install @types/node --save

If you now run the npm test, error messages will have cleared up and scripts will re-compile on the go, however make sure you restart the server to check that the error messages do not show up anymore.

To allow the server to refresh the changes on the go, use nodemon. Nodemon is an npm package that restarts the server any time application files change.

Install Nodemon:

 npm install -g nodemon

Once Nodemon has finished installing, instead of node main.js, run nodemon main.js. Change what shows on the browser from the simple JSON message to a couple of HTML tags, and go to the routes function in the app``**.ts** file to move the part where res.json appears on line 30, and change the whole block to this:

 res.send( `<html> <head> <title>Tutorial: HelloWorld</title> </head> <body> <h1>HelloWorld Tutorial</h1> <p> The current data and time is: <strong>`+new Date()+`</strong> </p> </body> </html>` )

That is it! You have created your first Express application using TypeScript. Your page should look like this:

Conclusion

Following the tutorial, you should have created your first simple application using TypeScript and Node.js. In summary, you should have learned how to use Gulp to automate the building process, as well as using Nodemon to restart the server. Once the scripts have re-compiled, you can decide to add more things like using view engines, serving static files, performing file uploads and a lot more.

The code base to this tutorial is available in a Public Github Repository. You can clone the repository and play around with the code base.


Tag cloud