A Nodejs REST-API server
This project implements two REST services: Users and Posts. You can create and authenticate a User as well as create Posts. It uses the Feathers, an open source web framework for building modern API and real-time applications.
Getting up and running is as easy as 1, 2, 3.
-
Install your dependencies
cd path/to/nodejs-rest-api yarn install
-
Rename and add your environment variables to the file .env. You can use the values pre-set for testing purposes.
cp .env.sample .env cat .env
SECRET_KEY=CHANGE-ME SERVER_PORT=3030 SERVER_PORT_TEST=3031 SERVER_URL=localhost SERVER_DB=sqlite://nodejs-rest-api.sqlite SERVER_DB_TEST=sqlite://nodejs-rest-api-test.sqlite
-
Test your app
yarn test
-
Start your app
yarn start
-
Make sure you have docker and docker-compose installed.
-
Install, test and start the App
cd path/to/nodejs-rest-api docker-compose up -d
-
You can also test your app directly
docker exec -it nodejs-rest-api yarn test
Once succeeded, access http://localhost:3030.
POST http://localhost:3030/users
Payload:
{
"username":"newuser",
"password":"mypassword"
}
Response:
{
"username":"newuser",
"id": <UUID>,
"accessToken": <JWT-Token>
}
POST http://localhost:3030/users/auth
Payload:
{
"username": "newuser",
"password": "mypassword",
"strategy": "local"
}
Response:
{
"accessToken": <JWT-Token>,
"authentication": {
"strategy": "local"
},
"user": {
"id": <UUID>,
"username": "newuser"
}
}
GET http://localhost:3030/users/{uuid}
{
"username":"newuser",
"id": <UUID>,
}
DELETE http://localhost:3030/users/{uuid}
Header:
Authorization: <JWT-Token>
POST http://localhost:3030/posts
Payload:
{
"title": "first post",
"body": "blah blah blah!"
}
Header:
Authorization: <JWT-Token>
Response:
{
"author_id": <user-UUID>,
"title": "first post",
"body": "blah blah blah!",
"id": <UUID>,
"updatedAt": <Timestamp>,
"createdAt": <Timestamp>
}
GET http://localhost:3030/posts/$limit={N}
Response:
{
"total": 1,
"limit": N,
"skip": 0,
"data": [
{
"author_id": <user-UUID>,
"title": "first post",
"body": "blah blah blah!",
"id": <UUID>,
"updatedAt": <Timestamp>,
"createdAt": <Timestamp>
}
]
}
The $limit param lets you set the number of posts to be returned.
The returned list is in the descending order by createdAt field.
GET http://localhost:3030/posts/{uuid}
Response:
{
"author_id": <user-UUID>,
"title": "first post",
"body": "blah blah blah!",
"id": <UUID>,
"updatedAt": <Timestamp>,
"createdAt": <Timestamp>
}
DELETE http://localhost:3030/posts/{uuid}
Header:
Authorization: <JWT-Token>