Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fonttech_base_server

FontTech35MIT1.0.101

This is a core base project for Font Tech server projects using MongoDB and Redis.

font, font tech, javascript, api, node, express, mongo, mongodb, jwt, postman, i18n, jwt-authentication, token, eslint, starter, web, app, rest, skeleton, async, await, mvp, front-end, testing, prettier, mocha, chai, redis, JSDoc

readme

Font Tech - Basic nodejs project skeleton

 ______    ____    _   _   _______     _                   _
|  ____|  / __ \  | \ | | |__   __|   | |                 | |
| |__    | |  | | |  \| |    | |      | |_    ___    ___  | |__
|  __|   | |  | | | . ` |    | |      | __|  / _ \  / __| | '_ \
| |      | |__| | | |\  |    | |      | |_  |  __/ | (__  | | | |
|_|       \____/  |_| \_|    |_|       \__|  \___|  \___| |_| |_|

Getting started

This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)

This project is created to help other developers create a basic REST API in an easy way with Node.js. This basic example shows how powerful and simple JavaScript can be. Do you want to contribute? Pull requests are always welcome to show more features.

Features

  • Multiple environment ready (development, production)
  • Custom email/password user system with basic security and blocking for preventing brute force attacks.
  • Compressed responses.
  • Secured HTTP headers.
  • CORS ready.
  • Cache ready (Redis).
  • HTTP request logger in development mode.
  • i18n ready (for sending emails in multiple languages).
  • User roles.
  • Pagination ready.
  • User profile.
  • Users list for admin area.
  • Cities model and controller example.
  • Login access log with IP, browser and country location (for country it looks for the header cf-ipcountry that CloudFlare creates when protecting your website).
  • API autogenerated documentation by Postman.
  • API collection example for Postman.
  • Testing with mocha/chai for API endpoints.
  • NPM scripts for cleaning and seeding the MongoDB database.
  • NPM script for keeping good source code formatting using prettier and ESLint.
  • Use of ESLint for good coding practices.
  • Mailer example with Nodemailer and Mailgun.
  • Ability to refresh token
  • JWT Tokens, make requests with a token after login with Authorization header with value Bearer yourToken where yourToken is the signed and encrypted token given in the response from the login process.

Requirements

  • Node.js 8+
  • MongoDB 3.6+
  • Redis 5.0+

Login credentials

email: `admin@admin.compassword:12345`

How to install

  1. Clone the project from github. Change "myproject" to your project name.
git clone https://gitlab.com/fontalland/fontbaseserver.git ./fontbaseserver

Using manual download ZIP

  1. Download repository
  2. Uncompress to your desired directory

Install npm dependencies after installing (Git or manual download)

cd myproject
npm install
npm update

Setting up environments (development or production)

  1. In the root this repository you will find a file named .env.example
  2. Create a new file by copying and pasting the file and then renaming it to just .env
  3. The file .env is already ignored, so you never commit your credentials.
  4. Change the values of the file to your environment (development or production)
  5. Upload the .env to your environment server(development or production)
  6. If you use the postman collection to try the endpoints, change value of the variable server on your environment to the url of your server, for development mode use http://localhost:3000

IMPORTANT: By default token expires in 3 days (4320 minutes set in .env.example). You can refresh token at endpoint GET /token. If everything it´s ok you will get a new token.

Mailer

To ensure the deliverability of emails sent by this API, Mailgun is used for mailing users when they sign up, so if you want to use that feature go sign up at their website https://www.mailgun.com

If you want to try a different method it´s ok, I used https://nodemailer.com for this API and they have different transport methods like: smtp.

i18n

Language is automatically detected from Accept-Language header on the request. So either you send locale manually on the request or your browser will send its default, if Accept-Language header is not sent then it will use en locale as default.

How to run

Database cleaning and seeding samples

There are 3 available commands for this: fresh, clean and seed.

npm run command
  • fresh cleans and then seeds the database with dynamic data.
  • clean cleans the database.
  • seed seeds the database with dynamic data.

Running in development mode (lifting API server)

npm run dev

You will know server is running by checking the output of the command npm run dev

****************************
*    Starting Server
*    Port: 3000
*    NODE_ENV: development
*    Database: MongoDB
*    DB Connection: OK
****************************

Running tests

It´s a good practice to do tests at your code, so a sample of how to do that in mocha/chai is also included in the /test directory

npm run test

Formatting code

Format your code with prettier by typing:

npm run format

Formatting markdown files

Format all your markdown files with remark by typing:

npm run remark

Linting code

Lint your code with ESLint by typing:

npm run lint

Usage

Once everything is set up to test API routes either use Postman or any other api testing application. Default username/password combination for login is `admin@admin.com/12345`.

API documentation

https://documenter.getpostman.com/view/487539/RWaHwoLV

Postman API example collection

You can import the example collection to Postman. To import, click the import button located and select postman-example.json located within the root directory.

Go to manage environments to create environments for development, production, etc. On each of the environments you create you will need to:

  1. Create a new key authToken and within the /login request this value is automatically updated after a successfull login through a script located in the tests tab. Each time you make a request to the API it will send Authorization header with the token value in the request, you can check this on the headers of users or cities endpoints in the Postman example.

  2. Create a second key server with the url of your server, for development mode use http://localhost:3000

This is a REST API, so it works using the following HTTP methods:

  • GET (Read): Gets a list of items, or a single item
  • POST (Create): Creates an item
  • PATCH (Update): Updates an item
  • DELETE: Deletes an item

Creating new models

If you need to add more models to the project just create a new file in /app/models/ and it will be loaded dynamically.

Creating new routes

If you need to add more routes to the project just create a new file in /app/routes/ and it will be loaded dynamically.

Creating new controllers

When you create a new controller file, try to also create another file with validations. Ex. countries.js and countries.validate.js. An example of this is included in the repository.

Developer Informations

Server init

  • To a fast server initialization
    ./init-server.sh

Mongo

  • To initialize mongdb service
    mongod --dbpath=/usr/local/var/mongodb/db --replSet rs0

Redis

  • Flush all data
    /usr/local/bin/redis-cli -p 6379 FLUSHALL

Redis Command

  • Install Command
npm install -g redis-commander
  • Run Command
    redis-commander --redis-port 6379 --nosave --open
  • To access in browser goes to http://localhost:8081/

ElasticSearch Command

  • Install ElasticSearch
cd /tmp
#cd config #elasticsearch.yml config port, authentication, etc. 9200
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-16.7.1.tar.gz
tar -zxvf elasticsearch-16.7.1.tar.gz
mv elasticsearch-16.7.1 /usr/local/opt/elasticsearch
cd /usr/local/opt/elasticsearch/
ln -s ../opt/elasticsearch/bin/elasticsearch /usr/local/bin/elasticsearch
ln -s ../opt/elasticsearch/bin/elasticsearch-cli /u sr/local/bin/elasticsearch-cli
ln -s ../opt/elasticsearch/bin/elasticsearch-env /usr/local/bin/elasticsearch-env
cd /usr/local/opt/elasticsearch/
elasticsearch

Kibana

  • To install Kibana
cd /tmp
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-darwin-x86_64.tar.gz
tar -zxvf kibana-6.5.4-darwin-x86_64.tar.gz
mv kibana-6.5.4-darwin-x86_64 /usr/local/opt/kibana
ln -s ../opt/kibana/bin/kibana /usr/local/bin/kibana

Global Access

  • To ngrok
    • Authenticate with ngrok doing
      ngrok authtoken PUT_HERE_THE_NGROK_ACCOUNT_TOKEN
    • To run the server with a defined domain: (ex: domain is fonttech)
      ./ngrok http 3000 -subdomain=fonttech -bind-tls=true
    • To run the server with a random domain:
      ./ngrok http 3000 -bind-tls=true
  • To serveo
    • Start the server running the command:
      ssh -o ServerAliveInterval=60 -R font-tech:80:localhost:3000 serveo.net

changelog

v7.1.2 (January 12, 2020)

  • Added cross-env to solve windows envionment issues
  • Use Mongoose built in function to validate ID
  • NPM updated

v7.1.1 (September 29, 2019)

  • Added new option MongoClient constructor
  • NPM updated

v7.1.0 (July 25, 2019)

  • Postman Collection example now included in root directory. Now /login has a test that automatically gets ans sets token. Thank you Glen! (#92)
  • NPM updated

v7.0.2 (July 12, 2019)

  • NPM updated

v7.0.1 (July 10, 2019)

  • NPM updated

v7.0.0 (July 3, 2019)

  • Making express-validator happy

v6.1.20 (July 3, 2019)

  • NPM updated

v6.1.19 (June 12, 2019)

  • NPM updated

v6.1.18 (June 5, 2019)

  • NPM updated

v6.1.17 (June 4, 2019)

  • NPM updated
  • Typos

v6.1.16 (May 27, 2019)

  • NPM updated

v6.1.15 (May 23, 2019)

  • NPM updated

v6.1.14 (May 22, 2019)

  • NPM updated

v6.1.13 (May 18, 2019)

  • Add role validation to User creator, Fixes #35

v6.1.12 (May 17, 2019)

  • NPM updated

v6.1.11 (May 14, 2019)

  • NPM updated

v6.1.10 (May 5, 2019)

  • NPM updated

v6.1.9 (Apr 24, 2019)

  • NPM updated

v6.1.8 (Apr 14, 2019)

  • NPM updated

v6.1.7 (Apr 11, 2019)

  • NPM updated

v6.1.6 (Apr 9, 2019)

  • README.md updated

v6.1.5 (Apr 9, 2019)

  • README.md updated

v6.1.4 (Apr 8, 2019)

  • NPM updated

v6.1.3 (Apr 7, 2019)

  • NPM updated

v6.1.2 (Apr 2, 2019)

  • NPM updated

v6.1.1 (Mar 30, 2019)

  • NPM updated

v6.1.0 (Mar 29, 2019)

  • ENHANCEMENT: Refresh token endpoint now works as GET instead of POST

v6.0.0 (Mar 28, 2019)

  • BREAKING CHANGE: Token payload and expiration have changed
  • BREAKING CHANGE: Constant in .env changed from JWT_EXPIRATION to JWT_EXPIRATION_IN_MINUTES
  • FEATURE: Refresh token

v5.0.1 (Mar 25, 2019)

  • NPM update

v5.0.0 (Mar 25, 2019)

  • Big refactor
  • FIX: send emails with mailgun

v4.0.14 (Mar 25, 2019)

  • Removed unused code

v4.0.13 (Mar 25, 2019)

  • FIX: remark

v4.0.12 (Mar 25, 2019)

  • FIX: Convert an email in request to lowercase

v4.0.11 (Mar 25, 2019)

  • README.md updated

v4.0.10 (Mar 25, 2019)

  • README.md updated

v4.0.9 (Mar 25, 2019)

  • README.md updated

v4.0.8 (Mar 24, 2019)

  • Removed normalizeEmail() function from validator.js. It was removing dots from email addresses. New function in utils to convert an email in request to lowercase. Fixes #11

v4.0.7 (Mar 18, 2019)

  • Travis CI changes

v4.0.6 (Mar 18, 2019)

  • CHANGELOG updated

v4.0.5 (Mar 18, 2019)

  • Fix itemAlreadyExists refactor

v4.0.4 (Mar 18, 2019)

  • itemAlreadyExists refactor

v4.0.3 (Mar 18, 2019)

  • itemAlreadyExists refactor

v4.0.2 (Mar 18, 2019)

  • itemNotFound refactor

v4.0.1 (Mar 18, 2019)

  • Refactor emailer

v4.0.0 (Mar 18, 2019)

  • Big refactor
  • NPM update

v3.0.4 (Mar 17, 2019)

  • Bumped to v3.0.4

v3.0.3 (Mar 17, 2019)

  • Use of remark to format markdown files

v3.0.2 (Mar 17, 2019)

  • Use of remark to format markdown files
  • Fix: use of parseInt now provides a base

v3.0.1 (Mar 15, 2019)

  • NPM updated
  • README.md updated

v3.0.0 (Mar 15, 2019)

  • Demo added

v2.3.3 (Mar 15, 2019)

  • Enable Redis based on env variable
  • API '/' route now renders an html view

v2.3.2 (Mar 14, 2019)

  • Test for npm publish

v2.3.1 (Mar 14, 2019)

  • Migrated to travis.com

v2.3.0 (Mar 13, 2019)

  • Added verification in response in test and development env
  • Added verification for default admin user in seeding
  • Added tests for cities and users with filters
  • All functions documentated with JSDoc
  • base.js renamed to utils.js

v2.2.8 (Mar 12, 2019)

  • Only builds in travis when tag is present

v2.2.7 (Mar 12, 2019)

  • Verification code is showed on development and testing environments
  • NPM updated

v2.2.6 (Mar 11, 2019)

  • Use of travis ci to automate deploy to npm
  • Added badge for tags in README.md

v2.2.2 (Mar 11, 2019)

  • Use of travis ci to automate build and deploy
  • Added badge for travis build in README.md

v2.2.1 (Mar 10, 2019)

  • Added badge for npm downloads in README.md

v2.2.0 (Mar 10, 2019)

  • Filtering from multiple fields redesigned

v2.1.10 (Mar 10, 2019)

  • NPM updated
  • FIX: creation of users were not saving data that validator was asking

v2.1.9 (Mar 9, 2019)

  • NPM run lint added

v2.1.8 (Mar 9, 2019)

  • New implementation for query on cities and users
  • More data on seeding
  • NPM updated

v2.1.7 (Mar 4, 2019)

  • More tests added

v2.1.6 (Mar 4, 2019)

  • Better testing

v2.1.5 (Mar 4, 2019)

  • Istambul nyc code coverage added

v2.1.4 (Mar 4, 2019)

  • Verification added only in tests responses at registration and forgot password
  • NPM updated
  • FIXED: User creation locale param was missing

v2.1.3 (Mar 2, 2019)

  • Verification removed from responses at registration and forgot password (They were being used for testing and somehow made it here)

v2.1.2 (Mar 2, 2019)

  • FEATURE: Install nodemon in devDependencies #9
  • Typos
  • README.md updated

v2.1.1 (Feb 27, 2019)

  • README.md updated

v2.1.0 (Feb 27, 2019)

  • i18n added for registration email and forgot password email
  • Typos

v2.0.1 (Feb 27, 2019)

  • Error response regression
  • NPM updated

v2.0.0 (Feb 24, 2019)

  • Breaking changes for success and error responses
  • Added new endpoint in /profile/changePassword
  • Fixes in tests
  • Fixes in validations
  • NPM updated

v1.2.12 (Feb 18, 2019)

  • NPM updated
  • CHANGELOG fixes
  • Typos

v1.2.11 (Feb 11, 2019)

  • NPM updated
  • Removed pm2 from start script in package.json
  • server.js now inits redis stuff only in production

v1.2.10 (Feb 9, 2019)

  • package.json updated

v1.2.9 (Feb 9, 2019)

  • CHANGELOG updated

v1.2.8 (Feb 9, 2019)

  • NPM updated

v1.2.7 (Dec 4, 2018)

  • FIXED: Error message standarization #6
  • Role property is returned in profile

v1.2.6 (Dec 1, 2018)

  • FIXED: Password length validation in profile #5
  • Role property is returned in profile

v1.2.5 (Dec 1, 2018)

  • Comments

v1.2.4 (Dec 1, 2018)

  • FIXED: Not standardized response on error #4
  • NPM updated

v1.2.3 (Nov 28, 2018)

  • FIXED: Password not encrypted when updating in profile #3
  • NPM updated

v1.2.2 (Nov 8, 2018)

  • NPM updated

v1.2.1 (Nov 5, 2018)

  • Cache API responses only in production mode

v1.2.0 (Nov 5, 2018)

  • Use of REDIS to cache API responses
  • NPM updated

v1.1.3 (Oct 24, 2018)

  • Seeding Fix due to changes on new mongo-seeding package

v1.1.2 (Oct 23, 2018)

  • NPM updated

v1.1.1 (Sep 28, 2018)

  • Clean and Seed with async/await
  • Fixes

v1.0.1 (Sep 21, 2018)

  • Added keywords to package.json

v1.0.0 (Sep 20, 2018)

  • First stable release