Skip to content

Commit

Permalink
DN-4: Testing API connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jun 24, 2024
1 parent 07d7127 commit 024ceb0
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 13 deletions.
22 changes: 12 additions & 10 deletions client/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001';

// only used with dynamic rendering, not compatible with output: 'export' | 'standalone'
// used locally for rewriting api calls to X server
async function rewrites() {
return [
{
source: '/api/:path*',
destination: `${API_URL}/api/:path*`
}
];
}
console.log(`Proxying API requests to ${API_URL}`);

const nextConfig = {
// output: 'export',
Expand All @@ -20,6 +11,17 @@ const nextConfig = {
// },
// output: 'standalone',
// assetPrefix: process.env.NEXT_PUBLIC_APP_URL || '', // if standalone
// ---
// only used with dynamic rendering, not compatible with output: 'export' | 'standalone'
// used locally for rewriting api calls to X server
async rewrites() {
return [
{
source: '/api/:path*',
destination: `${API_URL}/:path*`
}
];
},
compiler: {
styledComponents: true
}
Expand Down
35 changes: 35 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "client",
"private": true,
"scripts": {
"dev": "next dev -p 6969",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand All @@ -19,6 +19,7 @@
}
},
"dependencies": {
"axios": "^1.7.2",
"gray-matter": "^4.0.3",
"mdast-util-toc": "6.0.0",
"next": "^14.2.4",
Expand Down
46 changes: 46 additions & 0 deletions client/src/app/test-api/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use client';
import { useEffect, useState } from 'react';
import { api } from '@components/utils/api/api';

function Page() {
const [data, setData] = useState(null);
const [error, setError] = useState(null);

useEffect(() => {
async function fetchData() {
try {
console.log('Attempting to fetch from:', '/api');
const response = await api.get('/');
console.log('Response:', response.data);
setData(response.data);
} catch (error: any) {
console.error('Error fetching data:', error);
setError(error.message);
}
}

fetchData();
}, []);

if (error) {
return (
<>
<h1>Error</h1>
<p>Failed to fetch data: {error}</p>
</>
);
}

if (!data) {
return <h1>Loading...</h1>;
}

return (
<>
<h1>Hello world!</h1>
<p>{JSON.stringify(data)}</p>
</>
);
}

export default Page;
7 changes: 7 additions & 0 deletions client/src/utils/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from 'axios';

const api = axios.create({
baseURL: '/api'
});

export { api };
19 changes: 18 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ const VERSION: string = JSON.parse(
const app = express();

app.use(express.json());
app.use(cors());
app.use(
cors({
origin: function (origin, callback) {
const allowedOrigins = [
'http://localhost:3000',
'https://derecksnotes.com',
'https://dev.derecksnotes.com'
];
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true
})
);

const redisClient = new Redis(env.REDIS_URI);

Expand All @@ -43,6 +59,7 @@ app.use(
const buildTime = new Date().toISOString();

app.get('/', (req: Request, res: Response) => {
console.log('Consoling - GET /');
res.json({
name: "Dereck's Notes API",
ok: true,
Expand Down
2 changes: 1 addition & 1 deletion server/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const env = (name: string): string => {
export const BUILD_ENV = env('BUILD_ENV');
export const BUILD_ENV_BOOL = BUILD_ENV === 'PROD' ? true : false;
export const API_URL = env('API_URL');
export const EXPRESS_PORT: string = '3000';
export const EXPRESS_PORT: string = process.env.EXPRESS_PORT || '3000';
export const SESSION_SECRET = env('SESSION_SECRET');

// MONGO DB
Expand Down

0 comments on commit 024ceb0

Please sign in to comment.