Skip to content

A template express JavaScript application for serving a JSON API. Provides most commonly-required functionality but removes most of the noise of setup for the beginner.

Notifications You must be signed in to change notification settings

nbogie/academy-express-api-starter-js

 
 

Repository files navigation

Intermediate starter for javascript express APIs.

This is an intermediate starter template for node.js API apps written in JavaScript (not TypeScript) using Express.

It does not use express router, though you are free to make use of that.

Configuration on dev machine

Copy .env.example to .env and set any variables there appropriately:

  • DATABASE_URL to your database's connection string.

If you are not using a database, change server.js to remove any import of the support/db.js module.

Configuration on production environment

If you using a DB for this API, set the DATABASE_URL environment variable (e.g. on render.com this is done through the UI under settings/environment).

Change this README.md file!

If you have used this project as a template, remember to change this readme file to add your own documentation and remove anything you don't need.

Features

  • Database support:

    • connection-pool setup for node-postgres
    • loads DATABASE_URL from env variable. (And tries to load .env files with dotenv)
  • automated testing with vitest

    • example test
  • formatting with prettier

  • linting with eslint

  • workflow config for CI on github (see .github/workflows/ci-for-node.yaml )

  • jsconfig.json (to enable vscode error-reporting in type-checked js files)

  • .gitignore

  • logging with morgan

  • simplest demo of input validation with zod

Installation

Install dependencies

yarn

Running

In dev mode

yarn dev

Running in production

yarn start

Running tests

yarn test

Linting

yarn lint

Formatting with prettier

yarn format

However, it is suggested you install vscode's prettier extension and enable the user setting format on save. When formatting, VSCode will notice the .prettierrc and format according to those rules (and prettier's defaults).

checking linting, formatting, and tests together

yarn check-all

CI (linting, formatting check, automated tests)

This project includes a workflow file in .github which will cause CI to run on github.

Debugging with vscode

Set a breakpoint in the margin of any JS file, and use run-and-debug (ctrl-shift-d)'s Run and Debug button to start express. Bear in mind that a breakpoint set in a request handler won't cause express to pause until a matching request comes in!

Documenting your API

An simple example swagger document can be found in openapi-docs/openapi.json.

If you don't need it, it can be safely deleted.

A more thorough example repo is available and recommended for those wanting to learn how to maintain and serve documentation alongside their API.

Getting started coding

About

A template express JavaScript application for serving a JSON API. Provides most commonly-required functionality but removes most of the noise of setup for the beginner.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%