avatarMarcos Henrique da Silva

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4405

Abstract

lopment dependencies to our <code>package.json</code> as well. If you will be using a nice IDE for working like <a href="https://www.jetbrains.com/webstorm/">WebStorm</a>, that I highly recommend, then you will have a lot of auto-complete benefits meanwhile coding.</p><div id="b931"><pre><span class="hljs-string">"devDependencies"</span>: { <span class="hljs-string">"@types/express"</span>: <span class="hljs-string">"^4.17.2"</span>, <span class="hljs-string">"source-map-support"</span>: <span class="hljs-string">"^0.5.16"</span>, <span class="hljs-string">"tslint"</span>: <span class="hljs-string">"^6.0.0"</span>, <span class="hljs-string">"typescript"</span>: <span class="hljs-string">"^3.7.5"</span> }</pre></div><p id="004b">At your terminal, you can use the following <code>npm i --save-dev @types/express source-map-support tslint typescript</code> .</p><p id="bebf">I’ve pasted my current <code>package.json</code> with the versions to try to avoid version conflict for the future.</p><p id="a79c">Now we are ready to change the previous code! Let’s create a file called <code>app.ts</code> inside our app folder with the following:</p><div id="a590"><pre><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">'express'</span>; <span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> http <span class="hljs-keyword">from</span> <span class="hljs-string">'http'</span>;</pre></div><div id="96c9"><pre><span class="hljs-keyword">const</span> <span class="hljs-keyword">app</span>: express.Application = express(); <span class="hljs-keyword">const</span> server: http.Server = http.createServer(<span class="hljs-keyword">app</span>); <span class="hljs-keyword">const</span> port = 3000;</pre></div><div id="4278"><pre>app.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/'</span>, <span class="hljs-function">(<span class="hljs-params">req: express.Request, res: express.Response</span>) =></span> { res.<span class="hljs-title function_">status</span>(<span class="hljs-number">200</span>).<span class="hljs-title function_">send</span>(<span class="hljs-string">Server running at port <span class="hljs-subst">${port}</span></span>) });</pre></div><div id="8c3c"><pre>server.listen(port, () => { <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">Server running at port <span class="hljs-subst">${port}</span></span>) });</pre></div><p id="4b67">For the ones who were following the previous article, we had an <code>index.js</code> but now we want to use a Typescript to code. We are basically changing the syntax to use the best of Typescript to have a better code maintenance in the future.</p><blockquote id="ae6a"><p>Ok, perfect, but how should I execute my code now?</p></blockquote><p id="1d7d">Typescript need to be “compiled” in a final Javascript file. To do it easily, we should update our scripts at the <code>package.json</code> file like the following:</p><div id="fb17"><pre><span class="hljs-string">"scripts"</span>: { <span class="hljs-string">"tsc"</span>: <span class="hljs-string">"tsc"</span>, <span class="hljs-string">"start"</span> : <span class="hljs-string">"npm run tsc && node ./dist/app.js"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"echo "</span><span class="hljs-keyword">Error</span>: <span class="hljs-keyword">no</span> <span class="hljs-keyword">test</span> specified<span class="hljs-string">" && exit 1"</span> },</pre></div><p id="2a06">the <code>tsc</code> script will be a script that just compile the typescript files into Javascript. The <code>start</code> script will run <code>tsc</code> script and after run our javascript file that will be build at the <code>dist</code> folder. Don’t mind about the <code>test</code> script, it was auto-generated when you ran <code>npm init</code> (if you had followed our <a href="https://readmedium.com/another-expressjs-api-tutorial-for-2020-part-01-hello-world-969d3280d4c0">first article</a>).</p><p id="7081">Our final <code>package.json</code> will be more-less like this:</p><div id="b00a"><pre>{ <span class="hljs-string">"name"</span>: <span class="hljs-string">"expressjs-api-tutorial"</span>, <span class="hljs-string">"version"</sp

Options

an>: <span class="hljs-string">"0.0.1"</span>, <span class="hljs-string">"description"</span>: <span class="hljs-string">"Tutorial of how to create a REST API using ExpressJS"</span>, <span class="hljs-string">"main"</span>: <span class="hljs-string">"dist/app.js"</span>, <span class="hljs-string">"scripts"</span>: { <span class="hljs-string">"tsc"</span>: <span class="hljs-string">"tsc"</span>, <span class="hljs-string">"start"</span>: <span class="hljs-string">"npm run tsc && node ./dist/app.js"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"echo "</span><span class="hljs-keyword">Error</span>: <span class="hljs-keyword">no</span> <span class="hljs-keyword">test</span> specified<span class="hljs-string">" && exit 1"</span> }, <span class="hljs-string">"repository"</span>: { <span class="hljs-string">"type"</span>: <span class="hljs-string">"git"</span>, <span class="hljs-string">"url"</span>: <span class="hljs-string">"git+https://github.com/makinhs/expressjs-api-tutorial.git"</span> }, <span class="hljs-string">"keywords"</span>: [ <span class="hljs-string">"REST"</span>, <span class="hljs-string">"API"</span>, <span class="hljs-string">"ExpressJS"</span>, <span class="hljs-string">"NodeJS"</span> ], <span class="hljs-string">"author"</span>: <span class="hljs-string">"Marcos Silva"</span>, <span class="hljs-string">"license"</span>: <span class="hljs-string">"ISC"</span>, <span class="hljs-string">"bugs"</span>: { <span class="hljs-string">"url"</span>: <span class="hljs-string">"https://github.com/makinhs/expressjs-api-tutorial/issues"</span> }, <span class="hljs-string">"homepage"</span>: <span class="hljs-string">"https://github.com/makinhs/expressjs-api-tutorial#readme"</span>, <span class="hljs-string">"dependencies"</span>: { <span class="hljs-string">"express"</span>: <span class="hljs-string">"^4.17.1"</span> }, <span class="hljs-string">"devDependencies"</span>: { <span class="hljs-string">"@types/express"</span>: <span class="hljs-string">"^4.17.2"</span>, <span class="hljs-string">"source-map-support"</span>: <span class="hljs-string">"^0.5.16"</span>, <span class="hljs-string">"tslint"</span>: <span class="hljs-string">"^6.0.0"</span>, <span class="hljs-string">"typescript"</span>: <span class="hljs-string">"^3.7.5"</span> } }</pre></div><p id="d733">Now you can run at your terminal <code>npm run start</code> and see the magic happening ;)</p><p id="8716">Open your browser and try to access <i>localhost:3000</i> to get a message of server running and we are all good.</p><p id="6a33">That’s all for this article. At the next article we will start to create our first CRUD endpoint and things will finally start to make more sense.</p><p id="b6d3">If you have any doubts, feel yourself free to add a comment and I will try to reply as soon as possible.</p><p id="9191">Have a nice coding!</p><p id="b626">Thanks for reading and see you at the <a href="https://readmedium.com/another-expressjs-api-tutorial-for-2020-part-03-managing-your-user-endpoints-e5e8f1e9741d">next article</a>!</p><div id="b06f" class="link-block"> <a href="https://readmedium.com/another-expressjs-api-tutorial-for-2020-part-03-managing-your-user-endpoints-e5e8f1e9741d"> <div> <div> <h2>Another ExpressJS API tutorial for 2020, part 03— Managing your user endpoints</h2> <div><h3>In this third part of the articles, we would create our first CRUD endpoint for an user resource</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*eWYm3Dm7s0t8lLWYRW4pow.jpeg)"></div> </div> </div> </a> </div><p id="d01d">tips:</p><ul><li>Learn basic ECMAScript <a href="http://es6-features.org/">here</a>;</li><li>Read ExpressJS <a href="https://expressjs.com/en/4x/api.html">documentation</a>;</li><li>Book suggestion: <a href="https://amzn.to/2WGb1f5">Design Patterns</a> (it’s a must read for our field)</li><li>Brazilian? PT-BR Book suggestion: <a href="https://amzn.to/2U6uXWI">Design Patterns</a></li><li>The full project is already done <a href="https://github.com/makinhs/expressjs-api-tutorial/">here</a></li></ul></article></body>

Another ExpressJS API tutorial for 2021, part 02 — going to Typescript

Starting an API ExpressJS project using Typescript

Hi again! Hope that you are here because of our part 01 tour. If you are not following from the top, please make sure to understand that this is our second article about how to built an API using ExpressJS. If you are completely lost, please go back to our part 01 article.

At mine last article, I’ve explained how the articles will work and combine each other and showed how to start a basic skeleton of the API using ExpressJS and pure Javascript.

Today we are going to change the code to work with Typescript.

Here is the series of articles that we are writing:

  • Create your hello world ExpressJS API (Yes… we always need to start with a hello world)
  • Configure our hello world application to use Typescript (TS is cool, believe me ;) ) We are here now ;)
  • Create our first CRUD API endpoints for an user (Everyone wants a registered user in their applications, no?)
  • Create and configure your controllers (We will talk about it after)
  • Create and configure your services (What the h. is that?)
  • Middleware’s! (Yes, we need and use it everyday)
  • End to end testings!
  • Configuring a secure way to manage user permissions via API
  • Putting all together into a Docker container
  • Configuring a simple MongoDB to connect to our application
  • Configuring permission level to the application
  • Add logs with Winston!

So… let’s go! Please remember that the git repository is here,

Today we are focusing on the second section, when we will configure our application to be able to code using Typescript. You can have the final solution of our app in this branch link.

If you prefer to be coding meanwhile reading the article, please use this branch link from the previous article.

Ok, we are finally ready (thank god!)

At our root project fold, let’s create a file tsconfig.json and paste the following code;

{
 "compilerOptions": {
  "target": "es2016",
  "module": "commonjs",
  "outDir": "./dist",
  "strict": true,
  "esModuleInterop": true,
  "inlineSourceMap": true
 }
}

We will need to add some development dependencies to our package.json as well. If you will be using a nice IDE for working like WebStorm, that I highly recommend, then you will have a lot of auto-complete benefits meanwhile coding.

"devDependencies": {
   "@types/express": "^4.17.2",
   "source-map-support": "^0.5.16",
   "tslint": "^6.0.0",
   "typescript": "^3.7.5"
  }

At your terminal, you can use the following npm i --save-dev @types/express source-map-support tslint typescript .

I’ve pasted my current package.json with the versions to try to avoid version conflict for the future.

Now we are ready to change the previous code! Let’s create a file called app.ts inside our app folder with the following:

import express from 'express';
import * as http from 'http';
const app: express.Application = express();
const server: http.Server = http.createServer(app);
const port = 3000;
app.get('/', (req: express.Request, res: express.Response) => {
    res.status(200).send(`Server running at port ${port}`)
});
server.listen(port, () => {
    console.log(`Server running at port ${port}`)
});

For the ones who were following the previous article, we had an index.js but now we want to use a Typescript to code. We are basically changing the syntax to use the best of Typescript to have a better code maintenance in the future.

Ok, perfect, but how should I execute my code now?

Typescript need to be “compiled” in a final Javascript file. To do it easily, we should update our scripts at the package.json file like the following:

"scripts": {
    "tsc": "tsc",
    "start" : "npm run tsc && node ./dist/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

the tsc script will be a script that just compile the typescript files into Javascript. The start script will run tsc script and after run our javascript file that will be build at the dist folder. Don’t mind about the test script, it was auto-generated when you ran npm init (if you had followed our first article).

Our final package.json will be more-less like this:

{
 "name": "expressjs-api-tutorial",
 "version": "0.0.1",
 "description": "Tutorial of how to create a REST API using ExpressJS",
 "main": "dist/app.js",
 "scripts": {
  "tsc": "tsc",
  "start": "npm run tsc && node ./dist/app.js",
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "repository": {
  "type": "git",
  "url": "git+https://github.com/makinhs/expressjs-api-tutorial.git"
 },
 "keywords": [
  "REST",
  "API",
  "ExpressJS",
  "NodeJS"
 ],
 "author": "Marcos Silva",
 "license": "ISC",
 "bugs": {
  "url": "https://github.com/makinhs/expressjs-api-tutorial/issues"
 },
 "homepage": "https://github.com/makinhs/expressjs-api-tutorial#readme",
 "dependencies": {
  "express": "^4.17.1"
 },
 "devDependencies": {
  "@types/express": "^4.17.2",
  "source-map-support": "^0.5.16",
  "tslint": "^6.0.0",
  "typescript": "^3.7.5"
 }
}

Now you can run at your terminal npm run start and see the magic happening ;)

Open your browser and try to access localhost:3000 to get a message of server running and we are all good.

That’s all for this article. At the next article we will start to create our first CRUD endpoint and things will finally start to make more sense.

If you have any doubts, feel yourself free to add a comment and I will try to reply as soon as possible.

Have a nice coding!

Thanks for reading and see you at the next article!

tips:

Typescript
Expressjs
Api Development
Nodejs
Tutorial
Recommended from ReadMedium