Node.js学习笔记6——RESTful API

REST: Representational State Transfer

Create RESTful

first of all, create a json data resource: users.json

{
   "user1" : {
      "name" : "mahesh",
      "password" : "password1",
      "profession" : "teacher",
      "id": 1
   },
   "user2" : {
      "name" : "suresh",
      "password" : "password2",
      "profession" : "librarian",
      "id": 2
   },
   "user3" : {
      "name" : "ramesh",
      "password" : "password3",
      "profession" : "clerk",
      "id": 3
   }
}

Then we create RESTful APIs:

Node.js学习笔记6——RESTful API_第1张图片
RESTful APIs

List Users

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/listUsers', function (req, res) {
    fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
        console.log( data );
        res.end( data );
    });
})

var server = app.listen(8081, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

Then you can get user list from http://127.0.0.1:8081/listUsers

Add User

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/listUsers', function (req, res) {
    fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
        console.log( data );
        res.end( data );
    });
})

app.get('/addUser', function (req, res) {

  var user = req.query.user
  var name = req.query.name
  var pass = req.query.password
  var profession = req.query.profession
  var ids = req.query.id

  var userToAdd = new Object()
  userToAdd[user] = {
      "name" : name,
      "password" : pass,
      "profession" : profession,
      "id" : ids
  }

  console.log('add info ' + JSON.stringify(userToAdd))
  res.end( JSON.stringify(userToAdd));

})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port
  console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

Then you can add user using http://localhost:8081/addUser?user=user4&name=realank&password=1233456&profession=geek&id=4

Get User By ID

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/:id', function (req, res) {
   // 首先我们读取已存在的用户
   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
       data = JSON.parse( data );
       var user = data["user" + req.params.id] 
       console.log( user );
       res.end( JSON.stringify(user));
   });
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port
  console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

Then you can get specific user info using http://localhost:8081/2

Delete User by ID

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/deleteUser/:id', function (req, res) {

   // First read existing users.
   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
       data = JSON.parse( data );
       delete data["user" + req.params.id];
       
       console.log( data );
       res.end( JSON.stringify(data));
   });
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port
  console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

Then you can delete specific user by http://127.0.0.1:8081/deleteUser/2

你可能感兴趣的:(Node.js学习笔记6——RESTful API)