콘솔메세지추가
This commit is contained in:
12
server.js
12
server.js
@@ -20,8 +20,9 @@ app.use(express.json());
|
|||||||
let db;
|
let db;
|
||||||
|
|
||||||
async function initializeDB() {
|
async function initializeDB() {
|
||||||
|
const dbPath = path.join(__dirname, 'wifi_markers.db');
|
||||||
db = await open({
|
db = await open({
|
||||||
filename: path.join(__dirname, 'wifi_markers.db'),
|
filename: dbPath,
|
||||||
driver: sqlite3.Database
|
driver: sqlite3.Database
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -46,13 +47,16 @@ async function initializeDB() {
|
|||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
console.log('Database initialized with WAL mode enabled.');
|
console.log(`[DB] Database initialized at: ${dbPath}`);
|
||||||
|
console.log('[DB] WAL mode enabled for concurrent writes.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
app.get('/api/markers', async (req, res) => {
|
app.get('/api/markers', async (req, res) => {
|
||||||
|
console.log(`[API] GET /api/markers - Fetching markers from SQLite`);
|
||||||
try {
|
try {
|
||||||
const markers = await db.all('SELECT * FROM markers');
|
const markers = await db.all('SELECT * FROM markers');
|
||||||
|
console.log(`[API] Found ${markers.length} markers`);
|
||||||
// Convert isPublic from 0/1 to boolean
|
// Convert isPublic from 0/1 to boolean
|
||||||
const formattedMarkers = markers.map(m => ({
|
const formattedMarkers = markers.map(m => ({
|
||||||
...m,
|
...m,
|
||||||
@@ -67,6 +71,7 @@ app.get('/api/markers', async (req, res) => {
|
|||||||
|
|
||||||
app.post('/api/markers', async (req, res) => {
|
app.post('/api/markers', async (req, res) => {
|
||||||
const { id, name, ssid, password, lat, lng, securityType, iconType, description, addedBy, createdAt, isPublic } = req.body;
|
const { id, name, ssid, password, lat, lng, securityType, iconType, description, addedBy, createdAt, isPublic } = req.body;
|
||||||
|
console.log(`[API] POST /api/markers - Saving marker: ${name} (${ssid}) to SQLite`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.run(`
|
await db.run(`
|
||||||
@@ -74,6 +79,7 @@ app.post('/api/markers', async (req, res) => {
|
|||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`, [id, name, ssid, password, lat, lng, securityType, iconType, description || '', addedBy, createdAt, isPublic ? 1 : 0]);
|
`, [id, name, ssid, password, lat, lng, securityType, iconType, description || '', addedBy, createdAt, isPublic ? 1 : 0]);
|
||||||
|
|
||||||
|
console.log(`[API] Successfully saved marker: ${id}`);
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@@ -83,8 +89,10 @@ app.post('/api/markers', async (req, res) => {
|
|||||||
|
|
||||||
app.delete('/api/markers/:id', async (req, res) => {
|
app.delete('/api/markers/:id', async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
console.log(`[API] DELETE /api/markers/${id} - Deleting marker from SQLite`);
|
||||||
try {
|
try {
|
||||||
await db.run('DELETE FROM markers WHERE id = ?', [id]);
|
await db.run('DELETE FROM markers WHERE id = ?', [id]);
|
||||||
|
console.log(`[API] Successfully deleted marker: ${id}`);
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ const API_Base_URL = '/api/markers';
|
|||||||
|
|
||||||
export const apiService = {
|
export const apiService = {
|
||||||
getHotspots: async (): Promise<WiFiHotspot[]> => {
|
getHotspots: async (): Promise<WiFiHotspot[]> => {
|
||||||
|
console.log(`[API Call] GET ${API_Base_URL}`);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(API_Base_URL);
|
const response = await fetch(API_Base_URL);
|
||||||
if (!response.ok) throw new Error('Failed to fetch markers');
|
if (!response.ok) throw new Error('Failed to fetch markers');
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log(`[API Response] Received ${data.length} hotspots`, data);
|
||||||
return data;
|
return data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to load hotspots from API", e);
|
console.error("Failed to load hotspots from API", e);
|
||||||
@@ -17,6 +19,7 @@ export const apiService = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
saveHotspot: async (hotspot: WiFiHotspot): Promise<void> => {
|
saveHotspot: async (hotspot: WiFiHotspot): Promise<void> => {
|
||||||
|
console.log(`[API Call] POST ${API_Base_URL}`, hotspot);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(API_Base_URL, {
|
const response = await fetch(API_Base_URL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -26,6 +29,8 @@ export const apiService = {
|
|||||||
body: JSON.stringify(hotspot),
|
body: JSON.stringify(hotspot),
|
||||||
});
|
});
|
||||||
if (!response.ok) throw new Error('Failed to save marker');
|
if (!response.ok) throw new Error('Failed to save marker');
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(`[API Response] Save success:`, data);
|
||||||
// Trigger a storage event to refresh UI if needed (keeping compatibility)
|
// Trigger a storage event to refresh UI if needed (keeping compatibility)
|
||||||
window.dispatchEvent(new Event('storage_updated'));
|
window.dispatchEvent(new Event('storage_updated'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -34,11 +39,15 @@ export const apiService = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
deleteHotspot: async (id: string): Promise<void> => {
|
deleteHotspot: async (id: string): Promise<void> => {
|
||||||
|
const url = `${API_Base_URL}/${id}`;
|
||||||
|
console.log(`[API Call] DELETE ${url}`);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_Base_URL}/${id}`, {
|
const response = await fetch(url, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
});
|
});
|
||||||
if (!response.ok) throw new Error('Failed to delete marker');
|
if (!response.ok) throw new Error('Failed to delete marker');
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(`[API Response] Delete success:`, data);
|
||||||
window.dispatchEvent(new Event('storage_updated'));
|
window.dispatchEvent(new Event('storage_updated'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to delete hotspot", e);
|
console.error("Failed to delete hotspot", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user