Skip to content

Commit

Permalink
Update CORS configuration and server settings for global access
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyureno2374 committed Nov 20, 2024
1 parent c12ab02 commit 707abb6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 12 deletions.
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions
.env
.env.local
.env.development.local
.env.test.local
.env.production.local


# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
50 changes: 38 additions & 12 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,45 @@ const express = require('express');
const cors = require('cors');
const app = express();

const PORT = process.env.PORT || 8080;

// Расширенная настройка CORS
app.use(cors({
origin: [
'https://kyureno.dev',
'https://www.kyureno.dev',
'https://portfolio-kyurenoxd.vercel.app',
'http://localhost:3000'
],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
origin: '*',
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowedHeaders: ['X-CSRF-Token', 'X-Requested-With', 'Accept', 'Accept-Version', 'Content-Length', 'Content-MD5', 'Content-Type', 'Date', 'X-Api-Version', 'Authorization'],
optionsSuccessStatus: 200
}));

app.listen(PORT, '0.0.0.0', () => {
console.log(`Backend server is running on port ${PORT}`);
// Добавляем промежуточное ПО для парсинга JSON
app.use(express.json());

// Добавляем промежуточное ПО для всех маршрутов
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS,PATCH');
res.header('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization');
res.header('Access-Control-Allow-Credentials', true);

// Обработка preflight запросов
if (req.method === 'OPTIONS') {
return res.status(200).end();
}

next();
});

// Базовый маршрут для проверки работы API
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});

// Обработка ошибок
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Что-то пошло не так!');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Сервер запущен на порту ${PORT}`);
});
46 changes: 46 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": 2,
"builds": [
{
"src": "frontend/package.json",
"use": "@vercel/next"
},
{
"src": "backend/server.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/api/(.*)",
"dest": "backend/server.js"
},
{
"src": "/(.*)",
"dest": "frontend/$1"
}
],
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Access-Control-Allow-Credentials",
"value": "true"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,OPTIONS,PATCH,DELETE,POST,PUT"
},
{
"key": "Access-Control-Allow-Headers",
"value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization"
}
]
}
]
}

0 comments on commit 707abb6

Please sign in to comment.