[WIP] API Support

This commit is contained in:
Moe Poi ~ 2021-11-15 16:59:21 +07:00
parent b98fdb5a6c
commit 32ff0299ba
2 changed files with 21 additions and 0 deletions

View file

@ -7,8 +7,10 @@ app.use(express.static('public'))
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
const indexRouter = require('./routes/index'); const indexRouter = require('./routes/index');
const api = require('./routes/api');
app.use('/', indexRouter); app.use('/', indexRouter);
app.use('/api', api);
app.listen('3000', ()=> { app.listen('3000', ()=> {
console.log('Server sudah berjalan di port 3000') console.log('Server sudah berjalan di port 3000')

19
routes/api.js Normal file
View file

@ -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;