add BE and FE connection in vite template

pull/1104/merge
LeonOstrez 2 years ago
parent 9e6392c6a3
commit 57ea3231e2

@ -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 %}

@ -0,0 +1,11 @@
import axios, { AxiosResponse } from "axios";
export const fetchData = async (url: string): Promise<any> => {
try {
const response: AxiosResponse = await axios.get(url);
return response.data;
} catch (error: unknown) {
console.error("Error fetching data:", error);
throw error;
}
};

@ -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 (
<div className="relative min-h-screen bg-background">
{/* <Header /> */}
@ -11,8 +28,22 @@ function App() {
<main className="flex min-h-screen items-center justify-center p-4">
<div className="text-center">
<TriangleRight className="mx-auto h-24 w-24 text-primary animate-pulse" />
<h1 className="mt-6 text-3xl font-bold tracking-tight">Welcome to Your Website</h1>
<p className="mt-2 text-muted-foreground">A modern web experience</p>
{error ? (
// Show error message if there's an error
<div className="mt-6 text-red-500">
<h2 className="text-2xl font-bold">Error</h2>
<p>{error}</p>
</div>
) : data ? (
// Show data if it's available
<>
<h1 className="mt-6 text-3xl font-bold tracking-tight">{data}</h1>
<p className="mt-2 text-muted-foreground">A modern web experience</p>
</>
) : (
// Show loading state while waiting for data
<p className="mt-6 text-xl">Loading...</p>
)}
</div>
</main>
@ -22,4 +53,3 @@ function App() {
}
export default App
{% endraw %}

@ -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"
}
}

@ -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');

@ -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;

@ -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.");

@ -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",

Loading…
Cancel
Save