项目作者: manaraph

项目描述 :
A simple node API with express connected to a postgreSQL database
高级语言: JavaScript
项目地址: git://github.com/manaraph/node-api-with-postgres-DB.git
创建时间: 2019-04-04T16:53:57Z
项目社区:https://github.com/manaraph/node-api-with-postgres-DB

开源协议:MIT License

下载


node-api-with-postgres-DB

A simple node API with express connected to a postgreSQL database

Cloning Repository

git clone https://github.com/manaraph/node-api-with-postgres-DB.git

Setting up PostgreSQL database using the command line

  1. # Use the default user `postgres` Then enter your password for postgres
  2. > psql -U postgres
  3. # View your connection information
  4. > \conninfo
  5. # Create a new user `me` with with password `password`
  6. > CREATE ROLE me WITH LOGIN PASSWORD 'password';
  7. # Give user `me` access to create databases
  8. > ALTER ROLE me CREATEDB;
  9. # Lists all roles/users
  10. > \du
  11. # Exit from the default session
  12. > \q
  13. # Connect as user me, and login using the password: `password`
  14. > psql -d postgres -U me
  15. # Create a database `api`
  16. > CREATE DATABASE api;
  17. # Lists all avaialble database
  18. > \list
  19. # connect to the database `api`
  20. > \c api
  21. # Connected to `api=>` Create table users with two field and auto-incrementing primary id
  22. > CREATE TABLE users (
  23. ID SERIAL PRIMARY KEY,
  24. name VARCHAR(30),
  25. email VARCHAR(30)
  26. );
  27. # Insert 2 entries to users
  28. > INSERT INTO users (name, email)
  29. VALUES ('Jerry', 'jerry@example.com'),
  30. ('George', 'george@example.com');
  31. # Create table data
  32. > CREATE TABLE data(ID VARCHAR(100), NAME VARCHAR(50), ADDRESS VARCHAR(100),
  33. PHONE_NUMBER VARCHAR(100), EMAIL VARCHAR(100), COMPANY VARCHAR(100),
  34. DATE_REGISTERED VARCHAR(50), LAST_UPDATED VARCHAR(50))
  35. # Import data from `.\src\data\data.csv` to our data table
  36. > \COPY data(ID,NAME,ADDRESS,PHONE_NUMBER,EMAIL,COMPANY,DATE_REGISTERED,LAST_UPDATED)
  37. from '.\src\data\data.csv' delimiter ',' csv header;

Setting up the noSQL Database

  1. # Create json data table `jsondt`
  2. > CREATE TABLE jsondt( data JSON);
  3. ##Inserts the json data from data table as arrays
  4. # > INSERT INTO jsondt(data)
  5. # select json_build_array(
  6. # ID,NAME,ADDRESS,PHONE_NUMBER,EMAIL,
  7. # COMPANY,DATE_REGISTERED,LAST_UPDATED)
  8. # FROM data;
  9. #Inserts the json data from data table as arrays
  10. > INSERT INTO jsondt(data)
  11. SELECT json_build_object( 'id',ID,'name',NAME,'address',ADDRESS,
  12. 'phone',PHONE_NUMBER, 'email',EMAIL,'company',COMPANY,
  13. 'registered',DATE_REGISTERED,'updated',LAST_UPDATED) from data;
  14. # View all data on jsondt table
  15. > SELECT * FROM jsondt

Installation and running app with npm

  1. cd node-api-with-postgres-DB
  2. # install dependencies
  3. npm install
  4. # serve app at localhost:3000
  5. npm start

Installation and running app with yarn

  1. cd node-api-with-postgres-DB
  2. # install dependencies
  3. yarn
  4. # serve app at localhost:3000
  5. yarn start

API Endpoints

You can use postman or even curl to reach out to the following api endpoints:

URL Endpoint HTTP Request Resource Accessed Access Type
/ GET Display home with api info public
/data GET Retrieve json data public
/users GET Retrieve all users public
/users POST Create new user public
/users/ GET Retrieve a user by ID public
/users/ PUT Retrieve a user by ID public
/users/ DELETE Delete a user by ID public

Testing API with curl

  • If you are running this command from powershell and you encounter the error A positional parameter cannot be found that accepts argument run the command below to fix it.

    Remove-item alias:curl

  • See details of the fix above on this link.

  • Ensure that your server is running and connection has been established with the postgres database before running these request.

  1. # Display home with api info
  2. # This can also view on your browser by visiting the link => http://localhost:3000/
  3. > curl -v http://localhost:3000/
  4. # GET => Retrieve all users
  5. # This can also view on your browser by visiting the link => http://localhost:3000/users
  6. > curl -v http://localhost:3000/users
  7. # GET => Retrieve a users with id 1
  8. # This can also view on your browser by visiting the link => http://localhost:3000/users/1
  9. > curl -v http://localhost:3000/users/1
  10. # POST => Create a new user
  11. > curl --data "name=m4&email=m4@example.com" http://localhost:3000/users
  12. # PUT => Update user with ID 1
  13. > curl -X PUT -d "name=Kramer" -d "email=kramer@example.com" http://localhost:3000/users/1
  14. # DELETE => Delete user with ID 1
  15. > curl -X "DELETE" http://localhost:3000/users/1

Credits

Adapted from:

Resources: