Skip to content

Commit 009acb1

Browse files
committed
RESTful Web Services
1 parent 3d96551 commit 009acb1

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,141 @@ eventEmitter.emit('Sum', '10', '20');
23102310
<b><a href="#">↥ back to top</a></b>
23112311
</div>
23122312

2313-
#### Q. ***Explain RESTful Web Services in Node.js?***
2313+
## Q. ***Explain RESTful Web Services in Node.js?***
2314+
2315+
REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol.
2316+
It is an architectural style as well as an approach for communications purposes that is often used in various web services development. A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol.
2317+
2318+
**HTTP methods**
2319+
2320+
* `GET` − Provides read-only access to a resource.
2321+
* `PUT` − Creates a new resource.
2322+
* `DELETE` − Removes a resource.
2323+
* `POST` − Updates an existing resource or creates a new resource.
2324+
* `PATCH`− Update/modify a resource
2325+
2326+
**Principles of REST**
2327+
2328+
* Uniform Interface
2329+
* Stateless
2330+
* Cacheable
2331+
* Client-Server
2332+
* Layered System
2333+
* Code on Demand (optional)
2334+
2335+
*Example*:
2336+
2337+
**users.json**
2338+
2339+
```json
2340+
{
2341+
"user1" : {
2342+
"id": 1,
2343+
"name" : "Ehsan Philip",
2344+
"age" : 24
2345+
},
2346+
2347+
"user2" : {
2348+
"id": 2,
2349+
"name" : "Karim Jimenez",
2350+
"age" : 22
2351+
},
2352+
2353+
"user3" : {
2354+
"id": 3,
2355+
"name" : "Giacomo Weir",
2356+
"age" : 18
2357+
}
2358+
}
2359+
```
2360+
2361+
**List Users** ( `GET` method)
2362+
2363+
Let\'s implement our first RESTful API listUsers using the following code in a server.js file −
2364+
2365+
```js
2366+
var express = require('express');
2367+
var app = express();
2368+
var fs = require("fs");
2369+
2370+
app.get('/listUsers', function (req, res) {
2371+
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
2372+
console.log( data );
2373+
res.end( data );
2374+
});
2375+
})
2376+
2377+
var server = app.listen(3000, function () {
2378+
var host = server.address().address
2379+
var port = server.address().port
2380+
console.log("App listening at http://%s:%s", host, port)
2381+
});
2382+
```
2383+
2384+
**Add User** ( `POST` method )
2385+
2386+
Following API will show you how to add new user in the list.
2387+
2388+
```js
2389+
var express = require('express');
2390+
var app = express();
2391+
var fs = require("fs");
2392+
2393+
var user = {
2394+
"user4" : {
2395+
"id": 4,
2396+
"name" : "Spencer Amos",
2397+
"age" : 28
2398+
}
2399+
}
2400+
2401+
app.post('/addUser', function (req, res) {
2402+
// First read existing users.
2403+
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
2404+
data = JSON.parse( data );
2405+
data["user4"] = user["user4"];
2406+
console.log( data );
2407+
res.end( JSON.stringify(data));
2408+
});
2409+
})
2410+
2411+
var server = app.listen(3000, function () {
2412+
var host = server.address().address
2413+
var port = server.address().port
2414+
console.log("App listening at http://%s:%s", host, port)
2415+
})
2416+
```
2417+
2418+
**Delete User**
2419+
2420+
```js
2421+
var express = require('express');
2422+
var app = express();
2423+
var fs = require("fs");
2424+
2425+
var id = 2;
2426+
2427+
app.delete('/deleteUser', function (req, res) {
2428+
// First read existing users.
2429+
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
2430+
data = JSON.parse( data );
2431+
delete data["user" + 2];
2432+
console.log( data );
2433+
res.end( JSON.stringify(data));
2434+
});
2435+
})
2436+
2437+
var server = app.listen(3000, function () {
2438+
var host = server.address().address
2439+
var port = server.address().port
2440+
console.log("App listening at http://%s:%s", host, port)
2441+
})
2442+
```
2443+
2444+
<div align="right">
2445+
<b><a href="#">↥ back to top</a></b>
2446+
</div>
2447+
23142448
#### Q. ***What is the difference between mysql.createConnection() and mysql.createPool() in Node.js MySQL module?***
23152449
#### Q. ***how to handle file upload in node js?***
23162450
#### Q. ***Explain the terms body-parser, cookie-parser, debug, jade, morgan, nodemon, pm2, serve-favicon, cors, .env, checksum, fs-extra, moment in Express JS?***

0 commit comments

Comments
 (0)