A ridiculously simple sample project done in NodeJS.
branch | type of the application |
---|---|
hello-world | A Hello world application |
starter | A minimalistic starter template for your next NodeJS project |
rest-api | A sample express application exposing REST API from a postgres database. |
todoapp | A full todoapp sample with angular on the frontend and the express API on the backend. |
Just run git checkout <branch>
and start the app.
Install dependencies
# Using npm
npm install
# Or using yarn
yarn
Create a postgres
database & import the sample todoapp database schema.
$ createdb YOUR_DATABASE
Create a .env
file with your configuration.
$ cp .env.example .env
Then start the app
$ npm start
To run the database migration, run the following command
# Using npm
$ npm run migrate
# Using yarn
$ yarn migrate
To run the database seed, run the following command
# Using npm
$ npm run seed
# Using yarn
$ yarn seed
If you have node-foreman
installed globally you can do
$ nf start
Run npm run list:routes
to list the available API routes.
$ npm run list:routes
API Routes
----------------------------------
GET /api/todos
POST /api/todos
GET /api/todos/:id
DELETE /api/todos/:id
PUT /api/todos/:id
----------------------------------
Now you can use http clients eg: httpie to test your APIs:
Create a todo item:
$ http --json POST http://127.0.0.1:3000/api/todos title='Build a Todo App' completed=false
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 115
Content-Type: application/json; charset=utf-8
Date: Fri, 11 Nov 2016 19:24:44 GMT
ETag: W/"73-SJsR27NngqcsXB5jUHZaXw"
X-Powered-By: Express
{
"completed": false,
"description": null,
"id": "8",
"recorded_at": "2016-11-11T19:24:44.710Z",
"title": "Build a Todo App"
}
Now list your todos:
$ http GET http://127.0.0.1:3000/api/todos
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 117
Content-Type: application/json; charset=utf-8
Date: Fri, 11 Nov 2016 19:25:35 GMT
ETag: W/"75-ANfFIfwITLbg+u1p7PPiVA"
X-Powered-By: Express
[
{
"completed": false,
"description": null,
"id": "8",
"recorded_at": "2016-11-11T19:24:44.710Z",
"title": "Build a Todo App"
}
]
Did you see that? Awesome. Now try the PUT
and DELETE
APIs as well.
We'll be building a frontend app to make use of this todo API shortly.