This is an easy, basic and raw example of HOW to implement an API with Node, Express and PostgreSQL (with Sequelize ORM).
- Node 12+
- NPM
- PostgreSQL
- Sequelize ORM
- Optional: ElephantSQL account
To avoid issues with husky, first enable git hooks (and add our hook):
npx husky install npx husky add .husky/pre-commitThen, install the dependencies as usual:
npm installcreatedb userspsql usersCOPY users(id, firstname, lastname, age, gender, username, company, email, phone, address, created_at, updated_at) FROM '/Users/your-user/data/node-express-postgresql/users.csv' DELIMITER ',' CSV HEADER;pg_dump postgres://your-user:your-password@127.0.01/agency | psql postgres://your-user:your-password@your-endpoint.db.elephantsql.com/your-database-namenpm run devnpm run build npm start- Returns an object with the key data containing an array of objects with
40 records. - Supports query string:
- ?limit=integer
- ?offset=integer
curl http://127.0.0.1:3333/api/users{ "data": [ { "id": 1, "firstname": "Christian", "lastname": "Deackes", "age": 36, "gender": "Genderqueer", "username": "cdeackes0", "company": "Eayo", "email": "cdeackes0@mit.edu", "phone": "602-240-5463", "address": "53 Lakewood Plaza", "createdAt": "2020-11-30T08:00:00.000Z", "updatedAt": "2021-03-28T07:00:00.000Z" }, { "id": 2, "firstname": "Staford", "lastname": "Noice", "age": 27, "gender": "Female", "username": "snoice1", "company": "Oyoloo", "email": "snoice1@si.edu", "phone": "951-811-1800", "address": "18298 Crest Line Road", "createdAt": "2021-06-30T07:00:00.000Z", "updatedAt": "2021-07-14T07:00:00.000Z" } ] }- Returns
nrecord(s) wherenis the value (type: Number) of thelimitkey.
curl http://127.0.0.1:3333/api/users?limit=1 { "data": [ { "id": 1, "firstname": "Christian", "lastname": "Deackes", "age": 36, "gender": "Genderqueer", "username": "cdeackes0", "company": "Eayo", "email": "cdeackes0@mit.edu", "phone": "602-240-5463", "address": "53 Lakewood Plaza", "createdAt": "2020-11-30T08:00:00.000Z", "updatedAt": "2021-03-28T07:00:00.000Z" }, ] }Wrong type for n value will return all the users. Example: users?limit=%27Hello%27
- Returns from
n(PRIMARY KEY) wherenis the value (type: Number) of theoffsetkey.
curl http://127.0.0.1:3333/api/users?offset=10 { "data": [ { "id": 11, "firstname": "Goldie", "lastname": "Dany", "age": 88, "gender": "Female", "username": "gdanya", "company": "Devcast", "email": "gdanya@berkeley.edu", "phone": "954-161-7922", "address": "68 Drewry Plaza", "createdAt": "2021-03-28T07:00:00.000Z", "updatedAt": "2021-03-19T07:00:00.000Z" }, { "id": 12, "firstname": "Kial", "lastname": "Hamberstone", "age": 53, "gender": "Male", "username": "khamberstoneb", "company": "Skipfire", "email": "khamberstoneb@yellowpages.com", "phone": "896-244-3662", "address": "68425 Buell Point", "createdAt": "2020-10-11T07:00:00.000Z", "updatedAt": "2021-06-02T07:00:00.000Z" } ] }- Returns an object with a delay of 1 second (default)
- Supports query string:
- ?limit=integer
- ?offset=integer
curl http://127.0.0.1:3333/latency { "data": "Thanks for waiting 1 second" }- Increases latency (delay) to
nmilliseconds where, min:1000 and max:4000. Default value: 1000ms.
Wrong type for n value will produce a default delay of 1000ms.
curl http://127.0.0.1:3333/latency?delay=2000 { "data": "Thanks for waiting 2 seconds" }- Any other endpoint will retrieve an object
curl http://127.0.0.1:3333/ { "message": "Node.js, Express, and PostgreSQL API!" }