This is a tech demo demonstrating an API built on Node.JS in 2022, and now serves as a case study in the maintenance burden for an API built in NodeJS without significant feature work. For the purposes of comparing performance and ergonomics, we completed multiple implementations of the same API in this repository using different Node.JS web server frameworks.
- Node.JS HTTP Module (
plain) - Restify (
restify) - Fastify (
fastify) - Express (
express) - Koa (
koa)
Note - Neither the PostgreSQL backend nor the web server implementations are configured to be production-ready. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For more information, reach out to the team at desert.frog.solutions@gmail.com or desertfrogsolutions.com
-
Install PostgreSQL.
-
Start a database instance
/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgresBy default, the database listens to localhost:5432.
- Initialize
acronymapiuser with passwordpasswordand databasedefaultdb
CREATE USER acronymapi PASSWORD 'password';
CREATE DATABASE defaultdb WITH OWNER = acronymapi;
GRANT ALL PRIVILEGES ON DATABASE defaultdb TO acronymapi;- Run initdb script (generated from acronym.json by src/generate-initdb.js with
npm run generate-initdb.)
PGPASSWORD=password psql --file=./initdb.sql --username=acronymapi --dbname=defaultdbDefault configuration aligned with the setup above is provided - see example.config.json for a template. The default node-postgres environment variables are used to interface with the database. The authentication information for the Put and Delete endpoints is API_USER and API_PASSWORD.
Since the server uses nconf, it supports hierarchical configuration:
- command line arguments, parsed by yargs (e.g. --foo baz, also nested: --foo.bar=baz)
- environment variables
- config.json in the project root directory
All configuration sources that were found will be flattened into one object, so that sources earlier in this list override later ones.
Start the server you select (e.g. express) with Node:
npm run start:expressIf actively developing the server (or you just want hot reloading), use nodemon to start the server with
npm run dev:expressThe spec.js file contains functional tests in Mocha for the server with the PostgreSQL access stubbed out with Sinon. Run them with
npm run testIf actively developing the code, run Mocha in "watch" mode with
npm run test:watchIf running with a "live" and initialized PostgreSQL database (e.g. started with docker-compose up database, run tests without mocked DB calls with
npm --test_live_pg_server=true run testThe test results can be validated in the database by using e.g.
PGPASSWORD=password psql -h localhost -U acronymapi -d defaultdband queryingSELECT * FROM acronym WHERE name LIKE '%ACRONYM%';to find a PUTACRONYM and a POSTACRONYM
If running with a "live" NodeJS application (e.g. started with docker-compose up backend or npm run start:express etc.), run tests against the live endpoint with
npm --test_live_node_server=true run test:expressBenchmarks are automated using Autocannon; with a server running in another process, run benchmarks with the command