DEV Community

Cover image for Node JS + TypeScript - Super Hero API
Ganesh Raja
Ganesh Raja

Posted on • Edited on

3 2

Node JS + TypeScript - Super Hero API

So yesterday I get started with the basics of TypeScript. Today I wanted to Setup a Simple Node JS project with typescript.

So the project concept is very simple. I have a superhero.json file in the disk. Whenever we start the server it will load the file and store it as an array. When the user makes a request it will randomly pick one superhero and return it.

It was pretty fun and interesting. Hereafter I think I should ts for all my node projects.

You can find my Repo Link here
https://github.com/ganeshraja10/Latest-Tech-Learnings

import express, { Request, Response, Application } from 'express';
import fs from 'fs';
const app: Application = express();

const data: string = fs.readFileSync('./data/super_hero.json').toString();

const SuperHeroData: any = JSON.parse(data);

app.get('/', function (req: Request, res: Response) {
  const randomSuperHero = SuperHeroData[Math.floor(Math.random() * SuperHeroData.length)];
  res.json(randomSuperHero);
});

app.listen(3000, function () {
  console.log('App is listening on port 3000!');
});
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more