DEV Community

Nour Abdou
Nour Abdou

Posted on

Express + Typescript + Mongoose boilerplate

Build REST API with Express + Typescript + Mongoose boilerplate

πŸ› simple starter for newbies.

βš›οΈ My main goal is a simple and easy starter for new learners. I like you to be focused on the correct learning path and not spending hours in choosing the right project structure.

✨ Features

  • Beautiful Code as simple as possible.
  • Clear Structure inspired by MVC Model.
  • Local4ization implemention.
  • Authorization CRUD example.

πŸ“Ž Available on Github: express-typescript-mongoose-boilerplate


Table of Content

  • Introduction
    • Backend development
    • About API
  • Express + Typescript + Mongoose boilerplate
    • About NodeJS
    • ExpressJS
    • MongoDB
    • Localization with I18n

Introduction

You will know a bit about backend development frameworks and how to do so using ExpressJS & MongoDB with typescript.

Backend development

Any app or web service has two broad parts: the frontend (client side) and the backend (server side).

The backend development is the part that you can’t β€œsee”. It is the internal work including stuff like the server, the database, etc.

Before dive in the backend dev tools, there are things you should be comfortable with, such as: Networks, Internet, Databases, Computer server.
A networks is a collection of connected devices and computers; The Internet is the network of all networks; the database is where we store data in a well defined, accessible way and it has many types.
Server is a computer program or device that provides a service to another computer program and its user/client.

Tools used in backend development

  • scripting languages: PHP, Javascript, Ruby, etc.
  • programming languages: python, java, etc.
  • frameworks: NodeJS, Laravel, Django, flask, etc.
  • databases: MongoDB, MySQL, etc.

About API

(Application Programming Interface)
An API is a set of programming code that enables data transmission between one software product and another. It also contains the terms of this data exchange The API defines endpoints, and valid request and response formats.

Types

Open APIs

Open APIs, also known as external or public APIs, are available to developers and other users with minimal restrictions. They may require registration, and use of an API key, or may be completely open.

Internal APIs

Internal APIs are designed to be hidden from external users. They are used within a company to share resources. They allow different teams or sections of a business to consume each other’s tools, data and programs.

Partner APIs

Partner APIs are technically similar to open APIs, but they feature restricted access, often controlled through a third-party API gateway.

API specifications/protocols

Remote Procedure Call (RPC)

This protocol specifies the interaction between applications based on the client-server architecture.

Service Object Access Protocol (SOAP)

SOAP is a lightweight protocol for exchanging structured information in a decentralized, distributed environment. It is mostly used with enterprise web-based software to ensure the high security of transmitted data.

Representational State Transfer (REST)

The term REST was introduced by computer scientist Roy Fielding in a dissertation in 2000. Unlike SOAP, which is a protocol, REST is a software architectural style with six constraints for building applications that work over HTTP, often web services.
REST is considered a simpler alternative to SOAP, which many developers find difficult to use because it requires writing a lot of code to complete every task and following the XML structure for every message sent. REST follows another logic since it makes data available as resources. Each resource is represented by a unique URL, and one can request this resource by providing its URL.

gRPC

gRPC is an open-source universal API framework. With gRPC, the client application can directly call methods from a server application located on a different computer as if it was a local object. This makes it easier to create distributed services and applications.

GraphQL

The need for faster feature development, more efficient data loading due to increased mobile adoption, and a multitude of clients, made the developers look for other approaches to software architecture. GraphQL, initially created by Facebook in 2012 for internal use, is the new REST with organizations like Shopify, Yelp, GitHub and Coursera using it to build APIs.
GraphQL is a query language for APIs. It allows the client to detail the exact data it needs and simplifies data aggregation from multiple sources, so the developer can use one API call to request all needed data. Another special feature of GraphQL is that it uses a type system to describe data.


Express + Typescript + Mongoose boilerplate

simple starter for newbies.

My main goal with this project is a simple and easy starter for new learners. I like you to be focused on the correct learning path and not spending hours in choosing the right project structure.

Features

  • Beautiful Code as simple as possible.
  • Clear Structure inspired by MVC Model.
  • Localization implemention.
  • Authorization CRUD example.

project structure

src\
    |--mvc  
        |--models\              # Mongoose models (data layer)
        |--routes\              # Views routes
        |--controllers\         # Route controllers (controller layer)
        |--middlewares\         # Custom express middlewares
    |--helpers\             # Utility functions
    |--app.js               # Express app
    |--init\                # Init Express app
        |--locales\             # translation maps
        |--db.ts                # Init DB Connection
        |--localize.ts          # Init localization
        |--routes.ts            # Init Routes
        |--thApp.ts             # Express app
|--index.ts             # App entry point
Enter fullscreen mode Exit fullscreen mode

about NodeJS

Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications, built on Google Chrome's JavaScript Engine (V8 Engine). Node.js was developed by Ryan Dahl in 2009.

about Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node based Web applications.
Some of the core features of Express framework:

  • Allows to set up middlewares to respond to HTTP Requests.
  • Defines a routing table which is used to perform different * * actions based on HTTP Method and URL.
  • Allows to dynamically render HTML Pages based on passing arguments to templates.

More Info about Express Available from here

about MongoDB

MongoDB is a document database designed for ease of application development and scaling.

MongoDB stores data as JSON documents.

The document data model maps naturally to objects in application code, making it simple for developers to learn and use.

mongo document

The fields in a JSON document can vary from document to document. Compare that to a traditional relational database table, where adding a field means adding a column to the database table itself and therefore to every record in the database.

Documents can be nested to express hierarchical relationships and to store structures such as arrays.

Then

A collection is a group of documents.

If you are familiar with relational databases, you can think of a collection as a table. But collections in MongoDB are far more flexible. Collections do not enforce a schema, and documents in the same collection can have different fields.

creating a db in mongoose: here

Localization with I18n

Most frameworks leave it to you how translations are being loaded. You are responsible to detect the user language, to load the translations and push them into the framework.

I used i18next which is an internationalization-framework written in and for JavaScript.
with i18next-http-middleware


References:

  • Basic Concept Of Back-end Web Development. Available from: here
  • Types of APIs. Available from: here
  • What is an API. Available from: here
  • Node.js tutorial. Available from: here
  • MongoDB. Available from: here

Happy Learning ..
Happy Building ...

Top comments (0)