"name": "manh",
"password": "password2",
"profession": "giangvien",
"id": 2
}
This API is similar to addUser API, where you can get an input through req.body and then rely on the userID to delete that User from the Database. For simplicity, suppose we delete the user whose ID is 2.
var express = require ( 'express' ); var app = express (); var fs = require ( "fs" ); var id = 2 ; app . get ( '/deleteUser' , function ( req , res ) { // Dau tien, doc tat ca cac User dang ton tai. fs . readFile ( __dirname + "/" + "users.json" , 'utf8' , function ( err , data ) { data = JSON . parse ( data ); delete data [ "user" + 2 ]; 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 ( "Ung dung Node.js dang lang nghe tai dia chi: http://%s:%s" , host , port ) })
Call the above service using http://127.0.0.1:8081/deleteUser on the local computer. It will produce the following results:
{user1:
{ name: 'huong', {name: 'huong',
password: 'password1', password: 'password1',
profession: 'sinhvien', profession: 'sinhvien',
id: 1 }, id: 1},
user3:
{name: 'tuyen',
password: 'password3', password: 'password3',
profession: 'laptrinhvien', profession: 'laptrinhvien',
id: 3 } id: 3}
}
According to Tutorialspoint
Previous post: Express Framework in Node.js
Next lesson: Request object in Node.js