Skip to content
Express.js server that implements production-ready error handling and logging following latest best practices.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
config
nginx
prisma
src
tests
.dockerignore
.editorconfig
.eslintignore
.eslintrc.js
.gitignore
.graphqlconfig
.travis.yml
Dockerfile
Dockerfile.dev
LICENSE
README.md
debug.yml
docker-compose.yml
now.json
package-lock.json
package.json
prettier.config.js
test-env.yml

README.md

Build Status Known Vulnerabilities codecov.io License

This is an Express.js based Nodejs server that implements production-ready error handling and logging following latest best practices.

This is a backend server for my project that is COMING SOON. It inspired by an idea to quickly create a production ready project with all the required infrastructure at low cost yet with important security measures in place and ability to quickly scale in order to ship a quality product to early adopters. Ideal for quickly starting an app to validate ideas and scale if needed.

Features

This project uses Git hook to validate commit messages based on community standards

I am always open to your feedback

ToDo:

  • Authentication (Gmail, Facebook, LinkedIn, Twitter)
  • Password recovery
  • add/remove social accounts
  • GDPR ready (cookies, settings)
  • TypeScript (maybe)

Get started

Security in production

  • Closing prisma to the outside world(#closing-prisma-to-the-outside-world)

Additional settings

Set up environment variables

Rename *.sample.js files in /server/config directory:

  • default.sample.js -> default.js
  • production.sample.js -> production.js
  • test.sample.js -> test.js

More details on how config works see node-config. You may also find Securing production config files useful

Install and start Docker

  • Install Docker (if not yet installed) and make sure it runs
  • Run docker-compose up

Install dependencies

  • npm install

Create account and connect to MongoDB Atlas cloud instance using the FREE plan

  • Create an account with MongoDB Atlas using the FREE plan and follow instructions here

Set up Prisma

  • Prisma serves as an ORM and to make it work you need to provide MongoDB connection string (you should have it from the previous step by now) in the .env file in the project root.
  • Schema is defined here ./prisma/datamodel.prisma
  • Use npm run get-schema to download schema from endpoint to ./src/generated/prisma.graphql. Note that it will run automatically on prisma deploy or (npm run deploy) as it is set up in post-deploy hook in /prisma/prisma.yml
  • Prisma GraphQl playground is available here http://localhost:4466
  • Apollo server GraphQl playground - http://localhost:3030/graphql

Create account and connect to RedisLabs cloud instance using the FREE plan

  • Create an account with Redislabs using the FREE plan (choose Cloud Hosted - free up to 30MB) and follow instructions here

Create account and setup Sentry error tracking

  • Create a Sentry account here
  • Add your-sentry-dsn to all configuration files in /config dir
  sentry: {
    dsn: 'your-sentry-dsn',
  },

Create account and setup MailJet to send transactional emails

  • Create a MailJet account here
  • Add your-mailjet-api-key and your-mailjet-secret to all configuration files in /config dir
  mailjet: {
    api_key: 'your-mailjet-api-key',
    secret: 'your-mailjet-secret',
  },
  • Important!!! You need to use a domain-based email address as an email sender (e.g. your project's domain name) to ensure emails are delivered to the inbox. Otherwise, they will end up in spam (including example@gmail.com once). In your MailJet account you can verify your email and take additional measures (e.g.SPF and DKIM settings) to ensure your emails are delivered.

Kue UI dashboard

  • The dashboard is available under http://localhost:3050/active or via nginx http://localhost:3030/kue/active

Run server

  • npm run dev - development mode or
  • npm run start - production mode
  • docker-compose -f docker-compose.yml -f test-env.yml up - run server in test environment

Testing and linting

  • npm run test:unit - run unit tests
  • npm run test:int - run integration tests
  • npm run coverage - test coverage
  • npm run lint - lint

Making changes to Graphql schemas

  • update/add data model /prisma/datamodel.prisma
  • update/add schema /src/graphql/schema.graphql
  • update/add queries, mutations or subscriptions /src/graphql/resolvers/
  • run npm run deploy to update prisma data model and generate schema in /generated

Test error handling

  • navigate to /api/crash/ and click on any of the listed options

    Note, that crash routes for testing error handling are not available in production env

Error handling implementation explained

Closing prisma to the outside world

  • Add your secret in prisma/prisma.yml
secret: putYourSuperSecretTextHere

  • Add the same secret in src/db/prisma.js
secret: 'putYourSuperSecretTextHere',

  • run npm run deploy

Now the http://localhost:3030/graphql endpoint will work as expected.

However the http://localhost:4466 will return "Your token is invalid" error. To be able to use it, you need to generate an authorization token and use it in HTTP headers. Here is how you do it:

  • run npm run get-prisma-token
  • copy the generated token
  • insert the following HTTP headers (bottom left corner) in graphql playground under http://localhost:4466
{
  "Authorization":"Bearer 'generated token'" // mind the space between "Bearer" and the token
}

Pushing images to docker hub

To push images to Docker Hub you need to provide your Docker user name and password as environment variables. Refer to Travis documentation for more details

Once environment variables set, uncomment related lines in .travis.yml file

You can’t perform that action at this time.