This is the starter base for Pixels projects using Node.js & Express. The template is API first starting point without frontend.
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.
- Express - Node framework
- Objection.js - ORM
- Knex - Query builder
- SQL database of your choice
- Jest and Supertest - tests suites
- Nodemon - dev server
- Node.js
- Yarn
- PostgresQL, MySQL, SQLite or other Knex supported SQL database.
- Download the repository
- Install the DB driver you are going to use.
- Set up env file based on .env.example for DB access
- Create your own tables or run
knex migrate:latest && knex seed:run
to get example database set up. yarn run dev
for dev mode oryarn run start
for production
Possible database drivers:
yarn add pg
yarn add sqlite3
yarn add mysql
yarn add mysql2
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
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.
})
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.
The starter can be stripped down for other purposes.
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);
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
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