Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. // Import package
  2. const pg = require('pg');
  3. var restify = require('restify');
  4.  
  5. // Server connect configuration
  6. const config = {
  7. user: 'postgres',
  8. database: 'dbkaryawan',
  9. password: '123',
  10. host: 'localhost',
  11. port: 5432,
  12. max: 10,
  13. idleTimeoutMillis: 30000
  14. };
  15. // Server pool
  16. const pool = new pg.Pool(config);
  17.  
  18. // Restify
  19. const server = restify.createServer({
  20. name: 'myapp',
  21. version: '1.0.0'
  22. });
  23.  
  24. server.use(restify.plugins.acceptParser(server.acceptable));
  25. server.use(restify.plugins.queryParser());
  26. server.use(restify.plugins.bodyParser());
  27.  
  28. // Create
  29. server.post('/api/karyawan/post', (req, res, next) => {
  30. let data = req.body;
  31. console.log(data.nomor);
  32. let array = pool.query(`INSERT INTO karyawan (
  33. nomor,
  34. nomor_induk,
  35. nama,
  36. alamat,
  37. tanggal_lahir,
  38. tanggal_masuk
  39. )
  40. VALUES
  41. ('${data.nomor}','${data.nomor_induk}','${data.nama}','${data.alamat}','${data.tanggal_lahir}','${data.tanggal_masuk}');`);
  42.  
  43. res.send({"data" : array.rows});
  44. });
  45.  
  46. // Read
  47. server.get('/api/karyawan/get', async (req, res, next) => {
  48. let array = await pool.query('SELECT * from karyawan');
  49. res.send({
  50. "success": true,
  51. "data" : array.rows,
  52. "message" : "Get Data Karyawan",
  53. "code": 200
  54. });
  55. });
  56.  
  57. // Update
  58. server.put('/api/karyawan/put', (req, res, next) => {
  59. let data = req.body;
  60. console.log(data.nomor);
  61. let array = pool.query(`UPDATE public.karyawan
  62. SET nomor='${data.nomor}', nomor_induk='${data.nomor_induk}', nama='${data.nama}', alamat='${data.alamat}', tanggal_lahir='${data.tanggal_lahir}', tanggal_masuk='${data.tanggal_masuk}'
  63. WHERE nomor='${data.nomor}';`);
  64.  
  65. res.send({"data" : array.rows});
  66. });
  67.  
  68. // Delete
  69. server.del('/api/karyawan/del', (req, res, next) => {
  70. let data = req.body;
  71. console.log(data.nomor);
  72. let array = pool.query(`DELETE FROM public.karyawan
  73. WHERE nomor='${data.nomor}';`);
  74.  
  75. res.send({"data" : array.rows});
  76. });
  77.  
  78. // Restify server start
  79. server.listen(9000, function () {
  80. console.log('%s listening at %s', server.name, server.url);
  81. });
  82.  
  83. // By : Udin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement