hieutv199x / expressjs-api

An example Node.js REST-API built with TypeScript & Express.js

Home Page:https://larswaechter.dev/blog/nodejs-rest-api-structure/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

expressjs-api

This repository is a dummy Node.js REST-API built with TypeScript and Express. You might want to use it as codebase for your own Node project.

You can find a detailed explanation about the application's architecture on my blog.

๐Ÿ“Œ Packages

A shortened list of the Node modules used in this app:

๐Ÿ”ฎ Features

  • ACL (access control list)
  • Component-based architecture
  • Caching (Redis)
  • DB seeding
  • Mailing
  • MySQL
  • Testing

๐Ÿ“‚ Folder structure

Read more here.

  • src/api everything needed for the REST API
    • src/api/components component routers, controllers, models, tests and more
    • src/api/middleware API middleware
  • src/config global configuration files
  • src/services services for sending mails, caching, authentication and more
  • src/test test factory

๐Ÿ’ป Setup

Native

Requirements:

Installation:

  1. Run npm install
  2. Rename .env.example to .env and enter environment variables
  3. Run npm run build to compile the TS code
  4. Run npm start to start the application

You can reach the server at http://localhost:3000/api/v1/.

Docker

Requirements:

Installation:

  1. Rename .env.docker.example to .env.docker and enter environment variables
  2. Run docker-compose up to start the Docker containers

You can reach the server at http://localhost:3000/api/v1/.

Building

During the build process the following tasks are executed:

  • Compiling TS into JS
  • Copying mail HTML templates to dist directory
  • Merging component policy.json files into a single one in dist/output/policies.combined.json

The last two tasks are executed using Gulp as you can see in gulpfile.js.

Database seeding

In db/seed.sql you'll find a SQL script that can be used for seeding the database with dummy data. Make sure that the database and its tables were created before executing the script. The tables are created on application start.

You can load the script via a npm command: npm run seed. If you want to seed the database from a Docker container you must connect to it before: docker exec -it expressjs-api bash.

Read more about database seeding on my blog.

๐Ÿ”จ Tools

ACL

This application uses acl for permission management. Each component in src/api/components has its own policy.json which includes permissions for each role.

During the build process all these policy.json files get merged into a single one using a Gulp task as described more above.

MailHog

MailHog is an email testing tool for developers. You can use it as SMTP server to simulate the process of sending mails. MailHog is included as Docker image within the docker-compose.yml file.

Start the containers as described above and you can open the MailHog web interface at http://localhost:8025 where you'll find an overview of all sent emails.

:octocat: Community

About

An example Node.js REST-API built with TypeScript & Express.js

https://larswaechter.dev/blog/nodejs-rest-api-structure/

License:MIT License


Languages

Language:TypeScript 96.0%Language:JavaScript 2.5%Language:Shell 0.9%Language:HTML 0.3%Language:Dockerfile 0.3%