diff --git a/core/templates/tree/vite_react/client/package.json b/core/templates/tree/vite_react/client/package.json index 68fda32a5c..31c370e88e 100644 --- a/core/templates/tree/vite_react/client/package.json +++ b/core/templates/tree/vite_react/client/package.json @@ -1,4 +1,3 @@ -{% raw %} { "name": "vite_client", "private": true, @@ -40,6 +39,7 @@ "@radix-ui/react-toggle": "^1.1.0", "@radix-ui/react-toggle-group": "^1.1.0", "@radix-ui/react-tooltip": "^1.1.4", + "axios": "^1.7.8", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "cmdk": "^1.0.0", @@ -78,5 +78,3 @@ "vite": "^5.4.8" } } - -{% endraw %} \ No newline at end of file diff --git a/core/templates/tree/vite_react/client/src/api/api.ts b/core/templates/tree/vite_react/client/src/api/api.ts new file mode 100644 index 0000000000..8212f29709 --- /dev/null +++ b/core/templates/tree/vite_react/client/src/api/api.ts @@ -0,0 +1,11 @@ +import axios, { AxiosResponse } from "axios"; + +export const fetchData = async (url: string): Promise => { + try { + const response: AxiosResponse = await axios.get(url); + return response.data; + } catch (error: unknown) { + console.error("Error fetching data:", error); + throw error; + } +}; \ No newline at end of file diff --git a/core/templates/tree/vite_react/client/src/app.tsx b/core/templates/tree/vite_react/client/src/app.tsx index ed442b520a..3685fa761a 100644 --- a/core/templates/tree/vite_react/client/src/app.tsx +++ b/core/templates/tree/vite_react/client/src/app.tsx @@ -1,9 +1,26 @@ -{% raw %} -import { TriangleRight } from "lucide-react" +import { useState, useEffect } from "react"; +import { TriangleRight } from "lucide-react"; +import { fetchData } from "./api/api"; // import { Header } from "./components/header" // import { Footer } from "./components/footer" function App() { + const [data, setData] = useState(null); + const [error, setError] = useState(null); + + useEffect(() => { + const getData = async () => { + try { + const result = await fetchData("http://localhost:3000/"); + setData(result); + } catch (err) { + setError(err.message); + } + }; + + getData(); + }, []); + return (
{/*
*/} @@ -11,8 +28,22 @@ function App() {
-

Welcome to Your Website

-

A modern web experience

+ {error ? ( + // Show error message if there's an error +
+

Error

+

{error}

+
+ ) : data ? ( + // Show data if it's available + <> +

{data}

+

A modern web experience

+ + ) : ( + // Show loading state while waiting for data +

Loading...

+ )}
@@ -22,4 +53,3 @@ function App() { } export default App -{% endraw %} \ No newline at end of file diff --git a/core/templates/tree/vite_react/server/package.json b/core/templates/tree/vite_react/server/package.json index f9bc147e5b..5da4acc296 100644 --- a/core/templates/tree/vite_react/server/package.json +++ b/core/templates/tree/vite_react/server/package.json @@ -12,20 +12,21 @@ "author": "", "license": "ISC", "dependencies": { + "@anthropic-ai/sdk": "^0.27.3", + "axios": "^1.7.7", "bcrypt": "^5.1.1", "body-parser": "^1.20.2", "chart.js": "^4.4.1", "connect-flash": "^0.1.1", + "connect-mongo": "^5.1.0", + "cors": "^2.8.5", "csv-writer": "^1.6.0", "dotenv": "^16.4.1", "ejs": "^3.1.9", "express": "^4.18.2", "express-session": "^1.18.0", - "connect-mongo": "^5.1.0", "moment": "^2.30.1", "mongoose": "^8.1.1", - "axios": "^1.7.7", - "openai": "^4.63.0", - "@anthropic-ai/sdk": "^0.27.3" + "openai": "^4.63.0" } } diff --git a/core/templates/tree/vite_react/server/routes/auth-routes.js b/core/templates/tree/vite_react/server/routes/auth.js similarity index 94% rename from core/templates/tree/vite_react/server/routes/auth-routes.js rename to core/templates/tree/vite_react/server/routes/auth.js index a671fcabe7..84a00c14e5 100644 --- a/core/templates/tree/vite_react/server/routes/auth-routes.js +++ b/core/templates/tree/vite_react/server/routes/auth.js @@ -46,7 +46,7 @@ router.post('/auth/login', async (req, res) => { router.get('/auth/logout', (req, res) => { req.session.destroy(err => { if (err) { - console.error('Error during session destruction:', err); // gpt_pilot_debugging_log + console.error('Error during session destruction:', err); return res.status(500).send('Error logging out'); } res.redirect('/auth/login'); diff --git a/core/templates/tree/vite_react/server/routes/index.js b/core/templates/tree/vite_react/server/routes/index.js new file mode 100644 index 0000000000..b09bd305c6 --- /dev/null +++ b/core/templates/tree/vite_react/server/routes/index.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +// Root path response +router.get("/", (req, res) => { + res.status(200).send("Welcome to Your Website!"); +}); + +router.get("/ping", (req, res) => { + res.status(200).send("pong"); +}); + +module.exports = router; diff --git a/core/templates/tree/vite_react/server/server.js b/core/templates/tree/vite_react/server/server.js index 68d4129de9..521d194e63 100644 --- a/core/templates/tree/vite_react/server/server.js +++ b/core/templates/tree/vite_react/server/server.js @@ -4,7 +4,9 @@ const mongoose = require("mongoose"); const express = require("express"); const session = require("express-session"); const MongoStore = require('connect-mongo'); -const authRoutes = require("./routes/auth-routes"); +const basicRoutes = require("./routes/index"); +const authRoutes = require("./routes/auth"); +const cors = require("cors"); if (!process.env.DATABASE_URL || !process.env.SESSION_SECRET) { console.error("Error: DATABASE_URL or SESSION_SECRET variables in .env missing."); @@ -17,6 +19,7 @@ const port = process.env.PORT || 3000; // Middleware to parse request bodies app.use(express.urlencoded({ extended: true })); app.use(express.json()); +app.use(cors()); // Setting the templating engine to EJS app.set("view engine", "ejs"); @@ -68,14 +71,11 @@ app.use((req, res, next) => { next(); }); +// Basic Routes +app.use(basicRoutes); // Authentication Routes app.use(authRoutes); -// Root path response -app.get("/", (req, res) => { - res.status(200).send("Hello, world!"); -}); - // If no routes handled the request, it's a 404 app.use((req, res, next) => { res.status(404).send("Page not found."); diff --git a/core/templates/vite_react.py b/core/templates/vite_react.py index c21a1d554d..521c7f4b80 100644 --- a/core/templates/vite_react.py +++ b/core/templates/vite_react.py @@ -12,6 +12,7 @@ class ViteReactProjectTemplate(BaseProjectTemplate): "client/index.html": "Main HTML entry point with root div for React mounting and main.tsx script.", "client/package.json": "Client configuration with dependencies, scripts, and metadata.", "client/postcss.config.js": "PostCSS configuration with tailwindcss and autoprefixer plugins.", + "client/src/api/api.ts": "API utility function for fetching data from the server.", "client/src/app.css": "Main application styles including root layout and animations.", "client/src/app.tsx": "Main React component with layout structure and welcome message.", "client/src/components/footer.tsx": "Footer component with privacy and terms links.", @@ -80,7 +81,8 @@ class ViteReactProjectTemplate(BaseProjectTemplate): "server/.env": "This file is a configuration file in the form of a .env file. It contains environment variables used by the application, such as the port to listen on, the MongoDB database URL, and the session secret string.", "server/server.js": "This `server.js` file sets up an Express server with MongoDB database connection, session management using connect-mongo, templating engine EJS, static file serving, authentication routes, error handling, and request logging. [References: dotenv, mongoose, express, express-session, connect-mongo, ./routes/authRoutes]", "server/package.json": "Server configuration with dependencies, scripts, and metadata. [References: server.js]", - "server/routes/auth-routes.js": "This file defines routes for user authentication including registration, login, and logout. It interacts with a User model to handle user data and uses bcrypt for password hashing and comparison. [References: models/User.js]", + "server/routes/auth.js": "This file defines routes for user authentication including registration, login, and logout. It interacts with a User model to handle user data and uses bcrypt for password hashing and comparison. [References: models/User.js]", + "server/routes/index.js": "This file defines routes for the home page.", "server/routes/middleware/auth-middleware.js": "This file defines a middleware function called isAuthenticated, which checks if a user is authenticated based on the presence of a userId in the session object. If authenticated, it allows the request to proceed to the next middleware or route handler; otherwise, it returns a 401 status response indicating the user is not authenticated.", "server/models/User.js": "This file defines a Mongoose model for a user with fields for username and password. It includes a pre-save hook to hash the user's password before saving it to the database using bcrypt. [References: mongoose, bcrypt]", "server/services/llm.js": "The file `llm.js` implements functionality for interacting with two large language model (LLM) providers: OpenAI and Anthropic. It uses the `axios` library for HTTP requests and the `dotenv` library to manage environment variables for API keys. The file defines functions to send requests to both providers, handling retries in case of errors. The `sendLLMRequest` function serves as a unified interface to send messages to either provider based on the specified provider name. The file exports sendLLMRequest() function for use in other parts of the application.", @@ -91,11 +93,14 @@ class ViteReactProjectTemplate(BaseProjectTemplate): options_class = NoOptions options_description = "" relevant_files = [ + "server/server.js", + "server/routes/index.js", "client/components.json", "client/eslint.config.js", "client/index.html", "client/package.json", "client/postcss.config.js", + "client/src/api/api.ts", "client/src/app.css", "client/src/app.tsx", "client/src/components/footer.tsx",