loading...
Cover image for Node JS + TypeScript - Super Hero API

Node JS + TypeScript - Super Hero API

ganesh profile image Ganesh Raja Updated on ・1 min read

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!');
});

Posted on by:

Discussion

markdown guide