This is an easy, basic and raw example of HOW to implement an API with Node, Express and PostgreSQL (with Sequelize ORM).
To avoid issues with husky
, first enable git hooks
(and add our hook):
npx husky install
npx husky add .husky/pre-commit
Then, install the dependencies as usual:
npm install
createdb users
psql users
COPY 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-name
npm run dev
npm run build
npm start
40 records
.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"
}
]
}
n
record(s) where n
is the value (type: Number) of the limit
key.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
n
(PRIMARY KEY) where n
is the value (type: Number) of the offset
key.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"
}
]
}
curl http://127.0.0.1:3333/latency
{
"data": "Thanks for waiting 1 second"
}
n
milliseconds 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"
}
curl http://127.0.0.1:3333/
{
"message": "Node.js, Express, and PostgreSQL API!"
}