79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
const path = require('path')
|
|
const bcrypt = require('crypto');
|
|
|
|
const app = express();
|
|
const port = 3000;
|
|
|
|
// Create a new SQLite database in memory
|
|
const db = new sqlite3.Database('ruscodb');
|
|
|
|
// Create a table to store data
|
|
db.serialize(() => {
|
|
db.table
|
|
db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='Customers'`, (err, row) => {
|
|
if(err){
|
|
db.run("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, description TEXT, image_url TEXT, latitude REAL, longitude REAL)");
|
|
}
|
|
})
|
|
});
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
app.use(express.json());
|
|
// app.get('/', (req, res) => {
|
|
// res.sendFile('index.html')
|
|
// });
|
|
|
|
|
|
// Insert a new item into the database
|
|
app.get('/addRuschi', (req, res) => {
|
|
const { name, description, image_url, latitude, longitude } = req.query;
|
|
db.run("INSERT INTO ruschi (type, nome, descrizione, foto, lat, lng) VALUES (?, ?, ?, ?, ?)", [type, name, description, image_url, latitude, longitude], function (err) {
|
|
if (err) {
|
|
return console.error(err.message);
|
|
}
|
|
console.log(`A row has been inserted with rowid ${this.lastID}`);
|
|
res.send(`Item added with ID: ${this.lastID}`);
|
|
});
|
|
});
|
|
|
|
// Retrieve all items from the database
|
|
app.get('/getRuschi', (req, res) => {
|
|
db.all("SELECT * FROM ruschi", [], (err, rows) => {
|
|
if (err) {
|
|
return console.error(err.message);
|
|
}
|
|
res.json(rows);
|
|
});
|
|
});
|
|
|
|
app.get('/login', (req, res) => {
|
|
const username = req.body.username;
|
|
const hashedPassword = bcrypt.createHash('sha256').update(password).digest('base64'); //.digest('base64');
|
|
db.all("SELECT * FROM user WHERE " + username + ' AND ' + hashedPassword, [], (err, rows) => {
|
|
if (err) {
|
|
return console.error(err.message);
|
|
}
|
|
res.json('OK');
|
|
});
|
|
});
|
|
|
|
app.post('/register', (req, res) => {
|
|
console.log(req.body)
|
|
const username = req.body.username;
|
|
const password = req.body.password;
|
|
const hashedPassword = bcrypt.createHash('sha256').update(password).digest('base64'); //.digest('base64');
|
|
db.run('insert into users (username, password) VALUES (?, ?)', [username, hashedPassword], (err, rows) => {
|
|
if (err) {
|
|
return console.error(err.message);
|
|
}
|
|
res.json('OK');
|
|
});
|
|
});
|
|
|
|
|
|
// Start the server
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on http://localhost:${port}`);
|
|
});
|