DEV Community

Cover image for Node.js - Getting started with Typescript
Renato
Renato

Posted on

Node.js - Getting started with Typescript

Hi folks! How are you?

This project is just a simple and conceptual REST API trying to get the best of Typescript on Node.js using MongoDB. In this case I've created just a proof of concept. In order to run the project, please go to the end of this post, where you can find out the GitHub repository link.

I will explain the main parts of the project and you can reply this post with any question you have.

Let's start with the project structure

.
├── README.md
├── docs
│   └── OBJECTS\ API.postman_collection.json
├── docker
│   └── docker-compose-dev.yml
├── nodemon.json
├── package-lock.json
├── package.json
├── src
│   ├── constants
│   │   └── index.ts
│   ├── controllers
│   │   ├── object.ts
│   │   └── public.ts
│   ├── index.ts
│   ├── interfaces
│   │   └── object.ts
│   ├── models
│   │   └── object.ts
│   ├── router.ts
│   └── services
│       ├── object.ts
│       └── public.ts
└── tsconfig.json
Enter fullscreen mode Exit fullscreen mode

The root level files:

  • tsconfig.json: Typescript configurations (Visit https://aka.ms/tsconfig.json).
  • nodemon.json: nodemon configuration file.
  • package.json: project's dependencies.
  • package-lock.json: automatically generated that contains the dependencies' tree.

Folders:

  • /docker: used only if you don't have a MongoDB server and you wanna run it with docker. To achieve that you have to have Docker and Docker Compose installed and run docker-compose -f docker/docker-compose-dev.yml
  • /docs: there is a Postman json file that you can import to test the API.
  • /src: contains the core of the project.

Then inside of /src:

  • index.ts: define the API structure and routing and also set MongoDB connection.
  • /constants: constants that we need.
  • /controllers: manage and define the different routes, but they don't process any thing.
  • /interfaces: define the interfaces with data types.
  • /models: define the model we need to map to MongoDB.
  • /services: process information and connect to the db.

Notice that I'm starting using Typescript and I've just read a few pages about documentation and best practices. My main idea is to improve this code during the next weeks and also to start a kind of discussion with suggestions and comments.

Thanks for reading and have fun!
👩‍💻🧑‍💻


IMPORTANT: I was working based on this post mainly. Changes were inserted to improve some patterns according to my knowledge.


Get the full code on GitHub

Top comments (0)