2.0.1 • Published 3 years ago

alterna-contract-api v2.0.1

Weekly downloads
-
License
-
Repository
bitbucket
Last release
3 years ago

nodejs-boilerplate

This is a repository boilerplate for creating a NodeJS Alterna API. Currently is using git instead of npm as registry. It gives you a template with basic folder structure and test/eslit dependencies ready.

Using the boilerplate

These are the steps you need to follow for using this boilerplate:

  1. Create your new repository
  2. Clone this repository
  3. Move to this repository and run these commands:
    1. rm -rf .git
    2. git init
    3. git remote add origin git@bitbucket.org:flipenergia/your-new-and-fresh-repo.git
    4. git pull origin master
    5. git add .
    6. git commit -m "initial commit"
    7. git push -ff --set-upstream origin master

Once you have done these steps you can also rename the name of the directory containing the new repository: mv nodejs-boilerplate-api your-new-and-fresh-repo. Also is a good idea to update the package.json with all the properties related to the repository and the package as:

  • name
  • version (start with 0.0.1 is useful for prototyping)
  • description
  • repository.url
  • author
  • homepage

Deploying a version

Once you want to deploy a version you need to:

  • Commit all your changes
  • Increase the package version at package.json following semantic versioning
  • Commit this change with the version prefixed with v as commit message. For version 1.0.0: git commit -m "v1.0.0"
  • Run npm run tag at the root of the project.

Using the library

Let's say you are going to use the package alterna-log@1.0.0 in one of your projects. You need to copy the repository url for being imported through ssh and also being sure that the machine where you are going to import it has also the ssh key enabled in alterna-log repository.

Once you have all that sorted out you just need to run:

# You can also install it as dev dependency with npm install -D
npm install "git+ssh://git@bitbucket.org/flipenergia/alterna-log.git#v1.0.0"

The next time you deploy a new version for the library you just need to update the dependency version in the package.json and run npm run update alterna-log.

Scripts

  • lint: Executes the linter for the project following the config using ESlint.
  • tag: Generates automatically the tag for git based on package.json version and pull everything to remote.
  • test: Run the build script and then execute tests with Mocha and Chai.

Installation

fastify

  npm i fastify --save

fastify-env plugin

  • Which helps you with loading environment variables and setting defaults for each variable

    npm install --save fastify-env

Instructions

Execute the next command:

  node index.js

Output:

  Registered route: /
  Registered route: /api/blogs
  Registered route: /api/blogs/:id

  {:30,"time":1620383671607,"pid":20155,"hostname":"artemisa","msg":"Server listening at http://127.0.0.1:40235"}
  {"level":30,"time":1620383671608,"pid":20155,"hostname":"artemisa","msg":"Server listening on http://127.0.0.1:40235"}

Test the following calls in the browser (look at the port that has been opened on the server):

TO DO

  • Add integration with documentación of swagger
  • It always set the same port
  • Testing post, put, delete methods
  • Include mocha or jest tests to validate defined methods