Need help with the following ui5 application using node.js, sqlite. The server.js looks like this
// File: server.js
const express = require("express");
const sqlite3 = require("sqlite3").verbose();
const cors = require('cors');
//app.use(cors({
// origin: 'http://localhost:8080'
//}));
const app = express();
const PORT = process.env.PORT || 3000;
// Initialize SQLite in-memory database
const db = new sqlite3.Database(":memory:");
// Create table and insert sample data
db.serialize(() => {
db.run("CREATE TABLE IF NOT EXISTS MyTable (id INTEGER PRIMARY KEY, name TEXT)");
db.run("INSERT INTO MyTable (name) VALUES ('John')");
});
// API endpoint to fetch data from SQLite database
app.get("/api/data", (req, res) => {
db.all("SELECT * FROM MyTable", (err, rows) => {
if (err) {
console.error(err.message);
res.status(500).json({ error: "Internal Server Error" });
} else {
res.json(rows);
}
});
});
// Example of a new endpoint
app.get("/api/other-data", (req, res) => {
// This is just a placeholder example endpoint
res.json({ message: "This is some other data from the server." });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
After executing ui5 serve, Server started. Clinking on the URL generated http://localhost:8080, the application starts, trying the fetch data and getting the error Error fetching data: The requested resource was not found.
//View1.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"sap/m/MessageToast"
], function (Controller, JSONModel, MessageToast) {
"use strict";
return Controller.extend("ui5sql.ui5sql.controller.View1", {
onInit: function () {
},
fetchDataFromNodeServer: function() {
fetch("/api/data")
.then(response => {
if (!response.ok) {
if (response.status === 404) {
throw new Error('The requested resource was not found.');
} else {
throw new Error('Network response was not ok. Status: ' + response.status + ' - ' + response.statusText);
}
}
return response.text(); // Get the raw text of the response
})
.then(data => {
try {
var jsonData = JSON.parse(data); // Try to parse the JSON
var oModel = new JSONModel(jsonData);
this.getView().setModel(oModel, "sqliteData");
} catch (error) {
// If parsing fails, show an error message
MessageToast.show("Error parsing JSON data: " + error.message);
}
})
.catch(error => {
// Handle network errors and specific error cases
MessageToast.show("Error fetching data: " + error.message);
});
}
});
});
//View1.view.xml
<mvc:View controllerName="ui5sql.ui5sql.controller.View1" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">
<Page id="page" title="{i18n>title}">
<content>
<!-- Your UI controls go here -->
<Button text="Fetch Data" press="fetchDataFromNodeServer" />
<Table items="{/}">
<columns>
<Column>
<Text text="Column 1" />
</Column>
<!-- Add more columns as needed -->
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{Property1}" />
<!-- Bind more properties as needed -->
</cells>
</ColumnListItem>
</items>
</Table>
</content>
</Page>
</mvc:View>
In Console getting this strange error
-language=EN:1
Failed to load resource: the server responded with a status of 404 (Not Found)
Log-dbg.js:499 2024-04-24 22:20:02.938600 Connector (LrepConnector) failed call ‘loadFlexData’: Error: Not Found
Application startup continues without data from this storage. –