From 32ff0299ba82b372508e330cab5566b4d164f2ba Mon Sep 17 00:00:00 2001 From: moepoi Date: Mon, 15 Nov 2021 16:59:21 +0700 Subject: [PATCH] [WIP] API Support --- index.js | 2 ++ routes/api.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 routes/api.js diff --git a/index.js b/index.js index 9268cbd..7857267 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,10 @@ app.use(express.static('public')) app.set('view engine', 'ejs') const indexRouter = require('./routes/index'); +const api = require('./routes/api'); app.use('/', indexRouter); +app.use('/api', api); app.listen('3000', ()=> { console.log('Server sudah berjalan di port 3000') diff --git a/routes/api.js b/routes/api.js new file mode 100644 index 0000000..861cdea --- /dev/null +++ b/routes/api.js @@ -0,0 +1,19 @@ +const express = require('express') +const router = express.Router() +const db = require('../modules/db') + +router.get('/getproducts', (req, res) => { + const conn = db.connect(); + conn.execute('SELECT * FROM `product`', [], function(err, results) { + res.json(results); + }); +}) + +router.get('/getproduct', (req, res) => { + const conn = db.connect(); + conn.execute('SELECT * FROM `product` WHERE `model` = ?', [req.query.id], function(err, results) { + res.json(results); + }); +}) + +module.exports = router; \ No newline at end of file