Coder Social home page Coder Social logo

jonathas / node-project-skeleton Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 4.0 386 KB

This project contains the skeleton I usually have in my Node projects

License: MIT License

TypeScript 22.20% JavaScript 15.09% Shell 40.52% Makefile 14.45% HTML 2.75% Dockerfile 4.99%
nodejs typescript nginx nginx-proxy pm2 redis mongodb javascript apidoc gulp

node-project-skeleton's Introduction

Node Project Skeleton

Disclaimer (January of 2023)

I've implemented this example in 2017, when API development was very different from today. Now in the beginning of 2023 I thought about updating it but then decided to keep it as it is for historical reasons and because there are just way too many things I'd change, so it would be better to start a similar project from scratch.

Let me list below what I would do different if this was done today:

  • Use swagger (documentation in a json file) instead of apidoc
  • Follow conventional commits for writing the commit messages
  • Versioning done with the help of release-it
  • Drop gulp and use only typescript instead.
  • jest instead of mocha for the tests
  • jest instead of istanbul for code coverage
  • fastify instead of express
  • eslint instead of tslint
  • Enforce proper rules with eslint. Example here
  • nodemon is not needed anymore
  • bluebird is not needed anymore

Truth be told, nowadays I'd recommend to start an API in Node.js using Nest, which already gives you a well implemented architecture to start your project with. So I might be creating a similar project soon, but using Nest instead, and then we can compare the differences.

Intro

This project contains the skeleton I usually have in my Node projects.

What is used here: Express, Mocha, Istanbul, apiDoc, gulp, TypeScript, MongoDB, Redis, pm2, Docker, Nginx, SSL, etc.

Initial dependencies

Setting up

Inside the infra directory, there are 3 ways of running the infrastructure for the project:

  • standalone, which is for when you want to deploy all the containers in the same server.
  • server-ssl, which is when you want to deploy to servers behind a load balancer, but this load balancer doesn't accept ssl termination.
  • server, which is when you want to deploy to servers behind a load balancer and this load balancer accepts ssl termination, so the servers receive unencrypted requests from it.

Of course, you can always deploy to a PaaS instead, if you don't want to run the infrastructure by yourself.

If you want to run the project using the standalone or the server-ssl mode, you will need to have your SSL certificate and its private key inside infra/common/ssl. You can generate a self-singned one by following the tutorial inside the infra/common/ssl/test directory.

You also need to generate a dhparam.pem file, which can be done by running the gen_dhparam.sh script inside infra/common/ssl. This will take a while to run.

Inside api/process.yml you can configure how many instances pm2 will start.

Then you can install the packages inside the api directory:

$ yarn install

and generate the necessary files before starting the server:

$ ./node_modules/.bin/gulp

Running

If you choose to run the infra from server-ssl, you will have to start the database containers as well, as server-ssl is not standalone.

If you choose to run the infra inside the standalone directory, then it contains also the container for the database and cache.

The way you can run any of these infrastructures is by pointing docker-compose to the chosen yml file inside the infra directory:

$ docker-compose -f <your file here> up

Then you can use a service file like the one inside infra/standalone/host for configuring the infra to start on boot, in case the server restarts.

Just change the path inside of it so it points to the right directory, then copy it to /etc/systemd/system, reload and enable the daemon:

$ sudo systemctl daemon-reload
$ sudo systemctl enable docker-infra

Developing

If you want to run the project for development, the standalone infra is the best way to do it, with the docker-dev.yml file, as it is configured to reload pm2 on every code change:

$ cd infra/standalone
$ docker-compose -f docker-dev.yml up

Ps: Generate the SSL certificate before doing that.

node-project-skeleton's People

Contributors

dependabot[bot] avatar jonathas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.