Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import package
- const pg = require('pg');
- var restify = require('restify');
- // Server connect configuration
- const config = {
- user: 'postgres',
- database: 'dbkaryawan',
- password: '123',
- host: 'localhost',
- port: 5432,
- max: 10,
- idleTimeoutMillis: 30000
- };
- // Server pool
- const pool = new pg.Pool(config);
- // Restify
- const server = restify.createServer({
- name: 'myapp',
- version: '1.0.0'
- });
- server.use(restify.plugins.acceptParser(server.acceptable));
- server.use(restify.plugins.queryParser());
- server.use(restify.plugins.bodyParser());
- // Create
- server.post('/api/karyawan/post', (req, res, next) => {
- let data = req.body;
- console.log(data.nomor);
- let array = pool.query(`INSERT INTO karyawan (
- nomor,
- nomor_induk,
- nama,
- alamat,
- tanggal_lahir,
- tanggal_masuk
- )
- VALUES
- ('${data.nomor}','${data.nomor_induk}','${data.nama}','${data.alamat}','${data.tanggal_lahir}','${data.tanggal_masuk}');`);
- res.send({"data" : array.rows});
- });
- // Read
- server.get('/api/karyawan/get', async (req, res, next) => {
- let array = await pool.query('SELECT * from karyawan');
- res.send({
- "success": true,
- "data" : array.rows,
- "message" : "Get Data Karyawan",
- "code": 200
- });
- });
- // Update
- server.put('/api/karyawan/put', (req, res, next) => {
- let data = req.body;
- console.log(data.nomor);
- let array = pool.query(`UPDATE public.karyawan
- 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}'
- WHERE nomor='${data.nomor}';`);
- res.send({"data" : array.rows});
- });
- // Delete
- server.del('/api/karyawan/del', (req, res, next) => {
- let data = req.body;
- console.log(data.nomor);
- let array = pool.query(`DELETE FROM public.karyawan
- WHERE nomor='${data.nomor}';`);
- res.send({"data" : array.rows});
- });
- // Restify server start
- server.listen(9000, function () {
- console.log('%s listening at %s', server.name, server.url);
- });
- // By : Udin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement