ruscomap/ruscoapp/server.js

85 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-02-16 22:01:36 +01:00
const express = require('express');
2024-02-16 22:47:04 +01:00
const sqlite3 = require('sqlite3').verbose();
2024-02-17 00:26:29 +01:00
const path = require('path')
const bcrypt = require('crypto');
2024-02-16 22:01:36 +01:00
const app = express();
2024-02-16 22:47:04 +01:00
const port = 3000;
2024-02-16 22:01:36 +01:00
2024-02-16 22:47:04 +01:00
// Create a new SQLite database in memory
2024-02-17 01:20:29 +01:00
const db = new sqlite3.Database('ruscodb');
2024-02-16 22:01:36 +01:00
2024-02-16 22:47:04 +01:00
// Create a table to store data
db.serialize(() => {
2024-02-25 19:25:16 +01:00
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)");
}
})
2024-02-16 22:01:36 +01:00
});
2024-02-17 00:47:04 +01:00
app.use(express.static(path.join(__dirname, 'public')));
2024-02-17 01:20:29 +01:00
app.use(express.json());
2024-02-17 00:47:04 +01:00
// app.get('/', (req, res) => {
// res.sendFile('index.html')
// });
2024-02-17 00:26:29 +01:00
2024-02-16 22:47:04 +01:00
// Insert a new item into the database
2024-02-17 00:26:29 +01:00
app.get('/addRuschi', (req, res) => {
2024-02-16 22:47:04 +01:00
const { name, description, image_url, latitude, longitude } = req.query;
2024-02-17 00:26:29 +01:00
db.run("INSERT INTO ruschi (type, nome, descrizione, foto, lat, lng) VALUES (?, ?, ?, ?, ?)", [type, name, description, image_url, latitude, longitude], function (err) {
2024-02-16 22:47:04 +01:00
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}`);
2024-02-16 22:01:36 +01:00
});
2024-02-16 22:47:04 +01:00
});
// Retrieve all items from the database
2024-02-17 00:26:29 +01:00
app.get('/getRuschi', (req, res) => {
db.all("SELECT * FROM ruschi", [], (err, rows) => {
2024-02-16 22:01:36 +01:00
if (err) {
2024-02-16 22:47:04 +01:00
return console.error(err.message);
2024-02-16 22:01:36 +01:00
}
2024-02-16 22:47:04 +01:00
res.json(rows);
2024-02-16 22:01:36 +01:00
});
});
2024-02-25 19:44:46 +01:00
app.post('/login', (req, res) => {
2024-02-17 00:26:29 +01:00
const username = req.body.username;
2024-02-25 19:44:46 +01:00
const password = req.body.password;
2024-02-17 01:20:29 +01:00
const hashedPassword = bcrypt.createHash('sha256').update(password).digest('base64'); //.digest('base64');
2024-02-25 19:44:46 +01:00
db.all("SELECT * FROM users WHERE username = ? AND password = ?", [username, hashedPassword], (err, rows) => {
2024-02-17 00:26:29 +01:00
if (err) {
2024-02-25 19:44:46 +01:00
return console.error(err.message)
2024-02-17 00:26:29 +01:00
}
2024-02-25 19:44:46 +01:00
if(rows.length == 1) {
return res.json('Logged in')
}
return res.json('Incorrect username or password')
})
})
2024-02-17 00:26:29 +01:00
2024-02-17 00:47:04 +01:00
app.post('/register', (req, res) => {
2024-02-17 01:20:29 +01:00
console.log(req.body)
2024-02-17 00:26:29 +01:00
const username = req.body.username;
2024-02-17 01:20:29 +01:00
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) => {
2024-02-17 00:26:29 +01:00
if (err) {
return console.error(err.message);
}
res.json('OK');
});
});
2024-02-16 22:01:36 +01:00
// Start the server
2024-02-16 22:47:04 +01:00
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
2024-02-16 22:01:36 +01:00
});