Node.js RESTful API Example
30 |An example about how to create a RESTful API using Express.js. 32 | The four CRUD operations are provided: create, read, update and delete records. This 33 | server keeps an array of JSON objects in memory and runs the CRUD operations on it. 34 | If the server is restarted everything returns to the initial configuration (12 event 35 | objects)
36 |Installation
39 |-
40 |
- Install Dependencies 41 | 45 |
- Go to the project's root directory cd /my/path/to/directory 46 |
- Run npm install 47 |
- Start using it! npm start 48 |
Available end-points
51 |GET /events
52 |Gets all the available events (12 sample events). 53 |
54 |Example: See the JSON result
55 | 56 |GET /events/:id
57 |Obtains an event given its id.
58 |Example: See the JSON result
59 | 60 |POST /events
61 |Creates an event (be sure you are sending the headers via your library).
62 | 63 |Headers
64 | 65 |Content-Type : application/json
66 | 67 |Request body (raw)
68 | 69 |{
70 | "topics": "",
71 | "thumbnail": "/img/tr-3.jpeg",
72 | "url": "index.html",
73 | "overrideURL": "",
74 | "linkType": "",
75 | "title": "Created by Postman",
76 | "summary": "Lorem ipsum dolor sit amet"
77 | }
78 |
79 |
80 | PUT /events/:id
81 |Updates an existing event. The JSON object must be passed in the request body as raw. It 82 | returns an error in case the event doesn't exist.
83 | 84 |Headers
85 | 86 |Content-Type : application/json
87 | 88 |Request body (raw)
89 | 90 |{
91 | "id": "1",
92 | "topics": "",
93 | "thumbnail": "/img/tr-3.jpeg",
94 | "url": "index.html",
95 | "overrideURL": "",
96 | "linkType": "",
97 | "title": "Updated by Postman",
98 | "summary": "Lorem ipsum dolor sit amet"
99 | }
100 |
101 |
102 | DELETE /events/:id
103 |Removes an event given its id.
104 |