<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ryanworl</title>
    <description>The latest articles on DEV Community by ryanworl (@ryanworl).</description>
    <link>https://dev.to/ryanworl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F258%2Fabout.jpg</url>
      <title>DEV Community: ryanworl</title>
      <link>https://dev.to/ryanworl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ryanworl"/>
    <language>en</language>
    <item>
      <title>Building an API Backend with TypeScript and Express - Part One: Setup</title>
      <dc:creator>ryanworl</dc:creator>
      <pubDate>Sat, 17 Dec 2016 16:35:12 +0000</pubDate>
      <link>https://dev.to/ryanworl/building-an-api-backend-with-typescript-and-express---part-one-setup</link>
      <guid>https://dev.to/ryanworl/building-an-api-backend-with-typescript-and-express---part-one-setup</guid>
      <description>&lt;p&gt;&lt;em&gt;This post originally appeared on &lt;a href="https://www.worl.co"&gt;worl.co&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;TypeScript gives JavaScript developers a much-needed type-safe option for front-end development, but it works just as well in a Node environment. Node even includes many next-generation JavaScript features that older browsers don't ship, so you won't need any post-processing tools like Babel or Webpack/Browserify. In this tutorial I will assume you have a basic understanding of the command line and have a recent version of Node and NPM installed. I would also recommend using &lt;a href="https://code.visualstudio.com"&gt;Visual Studio Code&lt;/a&gt; because it has good out-of-the-box support for TypeScript.&lt;/p&gt;

&lt;p&gt;We're going to build a basic backend API with Node and TypeScript. We'll use the popular &lt;a href="https://expressjs.com"&gt;Express&lt;/a&gt; web framework for the basic building blocks. In future posts, we'll expand into database persistence, code organization, advanced TypeScript features, and more. In this post, we'll just cover getting our TypeScript environment set up and creating a "Hello World!" route with Express.&lt;/p&gt;

&lt;p&gt;If you don't already have TypeScript installed, you can install it with &lt;code&gt;npm install -g typescript&lt;/code&gt; in a terminal. At the time of writing, the current stable version is 2.1.&lt;/p&gt;

&lt;p&gt;Now we can get started! &lt;code&gt;cd&lt;/code&gt; into wherever you'd like to store this project, and run &lt;code&gt;mkdir ts-express; cd ts-express&lt;/code&gt;. Then run &lt;code&gt;npm init&lt;/code&gt; to create the &lt;code&gt;package.json&lt;/code&gt; file describing the application. You can leave the defaults as they are.&lt;/p&gt;

&lt;p&gt;You now have all the of the basic scaffolding to get started with TypeScript. It works very similarly to any other Node project, and your general workflow will be the same as a regular JavaScript project. We can now install our dependencies from NPM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save express body-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because these are JavaScript libraries, we need TypeScript definition files to get the full benefit of type-checking when we use them. These definition files declare the module layout and exported types for each library. You can install the definitions for Express and the body-parser middleware like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @types/express @types/body-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's write some TypeScript! Open up your editor of choice into the &lt;code&gt;ts-express&lt;/code&gt; directory and start by creating a &lt;code&gt;src&lt;/code&gt; directory. This directory will hold our TypeScript code. Later we'll configure where the TypeScript compiler should output our code. Once you've created the &lt;code&gt;src&lt;/code&gt; directory, create a new file called &lt;code&gt;app.ts&lt;/code&gt;. We're going to place all our code in this file to start with and learn more about code organization later.&lt;/p&gt;

&lt;p&gt;As a base, we'll start with a simple JavaScript version and slowly convert it to TypeScript. All valid JavaScript is valid TypeScript. If you have a legacy JavaScript project to convert, you can start by changing all the file extensions from &lt;code&gt;js&lt;/code&gt; to &lt;code&gt;ts&lt;/code&gt; and incrementally adding types until you're satisfied. Any compiler errors about types when compiling this un-typed code are really more like warnings: the TypeScript compiler just keeps going.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/app.ts

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.listen(3000, function() {
  console.log('Example app listening on port 3000!');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code starts an Express server, adds one root route, then starts listening on port 3000. That's about as stripped-down as you can get. Now let's compile it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tsc src/app.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously compiling all our files one at a time as we write more code is not pleasant, and we definitely don't want our compiled JavaScript sitting next to our TypeScript files. The TypeScript compiler has a configuration file to let us fix that.&lt;/p&gt;

&lt;p&gt;Here's the &lt;code&gt;tsconfig.json&lt;/code&gt; file I'm going to use for the remainder of this project. Put it in the root of the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": false,
    "removeComments": true,
    "preserveConstEnums": true,
    "outDir": "build",
    "strictNullChecks": true,
    "sourceMap": true,
    "target": "es2015"
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "**/*.spec.ts"
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;outDir&lt;/code&gt; tells the compiler to output our compiled code into a directory named &lt;code&gt;build&lt;/code&gt;. Don't worry about creating it, the compiler will do that for us. &lt;code&gt;strictNullChecks&lt;/code&gt; forces us to be correct about knowing if any of our variables can be null, which will save you some annoying debug time later when something is unexpectedly null or undefined. &lt;code&gt;target&lt;/code&gt; tells the compiler to compile our code into ES2015 JavaScript syntax. Other options include ES3 and ES5, which you would most likely use when targeting browsers because of their more limited feature set. In general, TypeScript tries to support as many previous versions as are reasonable and possible for their advanced features like &lt;code&gt;async/await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you can compile with &lt;code&gt;tsc&lt;/code&gt; and, hopefully, not receive any errors. Your code will output into the &lt;code&gt;build&lt;/code&gt; directory as specified in &lt;code&gt;tsconfig.json&lt;/code&gt;. You can now run the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tsc
$ node build/app.js
Example app listening on port 3000!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for this section! The next section will explore TypeScript's type-checking, defining different routes, and validation.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>express</category>
      <category>node</category>
    </item>
  </channel>
</rss>
