Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const mysql = require('mysql');
- const cors = require('cors');
- const bodyParser = require('body-parser');
- const app = express();
- const port = 3000;
- app.use(cors());
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(bodyParser.json());
- const db = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'scan_barcode',
- });
- db.connect((err) => {
- if (err) {
- console.log('Error connecting to MySQL:', err);
- } else {
- console.log('Connected to MySQL');
- }
- });
- app.post('/login', (req, res) => {
- const username = req.body.username;
- const password = req.body.password;
- const query = `INSERT INTO login (namauser, password) VALUES ('${username}', '${password}')`;
- db.query(query, (err, result) => {
- if (err) {
- console.log('Error executing query:', err);
- res.status(500).send('Internal Server Error');
- } else {
- console.log('User inserted successfully');
- res.status(200).send('User inserted successfully');
- }
- });
- });
- // Get product by kodeBarang
- app.get('/master_barang/:kodeBarang', (req, res) => {
- const kodeBarang = req.params.kodeBarang;
- const sql = 'SELECT * FROM master_barang WHERE kodeBarang = ? LIMIT 1';
- db.query(sql, kodeBarang, (err, result) => {
- if (err) throw err;
- if (result.length > 0) {
- res.send(result[0]);
- } else {
- res.status(404).send('Product not found');
- }
- });
- });
- // Add a new product
- app.post('/master_barang', (req, res) => {
- const product = req.body;
- const sql = 'INSERT INTO master_barang SET ?';
- db.query(sql, product, (err, result) => {
- if (err) throw err;
- res.send('Product added');
- });
- });
- // Delete a product by kodeBarang
- app.delete('/master_barang/:kodeBarang', (req, res) => {
- const kodeBarang = req.params.kodeBarang;
- const sql = 'DELETE FROM master_barang WHERE qty = 0 AND kodeBarang = ?';
- db.query(sql, kodeBarang, (err, result) => {
- if (err) throw err;
- res.send('Product deleted');
- });
- });
- app.listen(port, () => {
- console.log(`Server is running on port ${port}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement