Giter Club home page Giter Club logo

pixels-node-starter's Introduction

Pixels Node Starter

This is the starter base for Pixels projects using Node.js & Express. The template is API first starting point without frontend.

Issues, improvements and these instructions.

Please read the documentation below before using. If things are not clear or you find a mistake, or a way to improve the template, please submit an issue or pull request.

Tools and Technologies

Requirements

Installation

  1. Download the repository
  2. Install the DB driver you are going to use.
  3. Set up env file based on .env.example for DB access
  4. Create your own tables or run knex migrate:latest && knex seed:run to get example database set up.
  5. yarn run dev for dev mode or yarn run start for production

Possible database drivers:

yarn add pg
yarn add sqlite3
yarn add mysql
yarn add mysql2

Example models & controllers

The repository has simple models with relations and controllers.

  • Person, Project and Task models correspond with tables in database.
  • Persons, Projects and Tasks controllers have endpoints for GET, POST, PUT and DELETE HTTP methods.
  • Project can have many tasks
  • Task can be assigned to one project

Auth

Project contains JWT based service for locking endpoints behind login. Use /api/login endpoint to login with username & password. Endpoint returns JWT token, that you can include with authorization headers. Example database has user root with password root.

Header key: Authorization
Header value: Bearer YOUR_JWT_FROM_RESPONSE

App has middleware for authenticating tokens. To enable token authentication for endpoint, check truthiness of request.authorized.

yourRouter.post('/:id', async (request, response, next) => {
  if( request.authorized ) {
  	// Your business logic here
  } else {
  	// Your error handling
  }
})

Or you can use requireAuth middleware to handle auth logic

// Auth middleware
const { requireAuth } = require('../middleware/requireAuth')

yourRouter.post('/:id', requireAuth, async (request, response, next) => {
  // Your ordinary business logic.
})

Tests & lint

Project comes with Eslint, Jest & Supertest. Eslint is configured to follow AirBnb-styleguide.

Lint commands: yarn run lint --> lint errors yarn run fix --> autofix fixable errors

Test commands: yarn run tests::prepare --> drops old test DB, creates new one from migrations & test seeds. yarn run test --> Runs prepare & jest tests.

tests folder has example tests for one API endpoint using Jest & Supertest.

Other use cases

The starter can be stripped down for other purposes.

Cloud Function

Starter can pretty easily be modified to run as cloud function.

As Firebase Cloud Function:

Swap index.js for Firebase cloudfunction index.

const functions = require('firebase-functions');
const app = require('./app')

// Hook starter on cloud function.
exports.app = functions.https.onRequest(app);

Non-database app

If you don't need database portion, just remove following components:

  • PostGres / MySQL / SQLite
  • Knex & Models directory
  • Database directory
  • References to DB in main App

Project structure

pixels-node-starter/                   # -> Project root, configs
|-- src/
  |-- controllers/                     # -> Controllers for endpoints
  |-- database/                        # -> Database migrations & seeds
    |-- migrations/                    # -> Knex migrations
    |-- seeds/                         # -> Knex seeds
  |-- middleware/                      # -> Express middleware
  |-- models/                          # -> Objection.js DB models
  |-- routes/                          # -> Route declarations
  |-- services/                        # -> Services & business logic
  |-- utils/                           # -> Misc. utils
  |-- app.js                           # -> Express app
|-- index.js                           # -> Server entry point

pixels-node-starter's People

Contributors

stscoundrel avatar dependabot[bot] 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.