Skip to content

Commit 68a804f

Browse files
committed
Improve cors on javascript-backend
1 parent e2c0512 commit 68a804f

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

backend-javascript/.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ APP_VERSION=1.1.1
66
# === SERVEUR APPLICATION ===
77
HOST=localhost
88
PORT=3000
9-
CORS_ORIGIN=http://localhost:3000
9+
# CORS_ORIGIN=http://localhost:4200
10+
CORS_ORIGIN=*
1011

1112
# === BASE DE DONNEES ===
1213
DB_CLIENT=mock # pg | mysql | mock

backend-javascript/.env.production

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ APP_VERSION=1.1.1
66
# === SERVEUR APPLICATION ===
77
HOST=localhost
88
PORT=8080
9-
CORS_ORIGIN=https://www.mon-domaine-en-prod.com
9+
# CORS_ORIGIN=https://www.mon-domaine-en-prod.com
10+
CORS_ORIGIN=*
1011

1112
# === BASE DE DONNEES ===
1213
DB_CLIENT=mock # pg | mysql | mock

backend-javascript/app.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import express from 'express';
2+
import cors from 'cors';
3+
4+
const app = express();
5+
6+
app.use(cors());
7+
8+
app.use(express.json());
9+
10+
app.get('/persons', (req, res) => {
11+
res.json({
12+
"success": true,
13+
"metadata": {
14+
"pagination": {
15+
"currentPage": 1,
16+
"perPage": 10,
17+
"totalItems": 4,
18+
"totalPages": 1
19+
}
20+
},
21+
"data": [
22+
{
23+
"id": 1,
24+
"name": "Steven Spielberg-javascript-backend-mock",
25+
"city": "Cincinnati"
26+
},
27+
{
28+
"id": 2,
29+
"name": "Ridley Scott-javascript-backend-mock",
30+
"city": "South Shields"
31+
},
32+
{
33+
"id": 4,
34+
"name": "Denis Villeneuve-javascript-backend-mock",
35+
"city": "Bécancour"
36+
},
37+
{
38+
"id": 3,
39+
"name": "Christopher Nolan-javascript-backend-mock",
40+
"city": "London"
41+
}
42+
]
43+
});
44+
});
45+
46+
// Démarrage du serveur
47+
app.listen(3000, () => {
48+
console.log('Backend démarré sur http://localhost:3000');
49+
});

backend-javascript/src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import compression from 'compression';
3+
import cors from 'cors';
34

45
import appConfig from './config/app.config.js';
56

@@ -28,6 +29,7 @@ app.use(compression());
2829
app.use(express.json());
2930
app.use(express.urlencoded({ extended: true }));
3031
app.use(initLocals);
32+
3133
configureSecurity(app);
3234

3335
if (['development', 'test'].includes(appConfig.app.nodeEnv)) {

backend-javascript/src/middlewares/security/security.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import appConfig from '../../config/app.config.js';
55

66
export default function configureSecurity(app) {
77
app.use(cors({ origin: appConfig.security.corsOrigin }));
8+
// app.use(cors({ origin: '*' }));
89
app.use(helmet(appConfig.security.helmet));
910

1011
const limiter = rateLimit({

frontend-angular/src/app/modules/features/crud/person/services/items-api.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class ItemsApiService implements ItemsServiceInterface {
2121
getItems(filters: Filters = {}): Observable<ItemsResponse> {
2222
const params = this.buildQueryParams(filters);
2323
const url = `${this.backendUrl}/${ITEM_CONSTANTS.RESOURCE_NAME}${params}`;
24-
2524
return this.http.get<ItemsResponse>(url).pipe(
2625
catchError(this.handleError('getItems', getDefaultItemsResponse()))
2726
);
@@ -47,4 +46,5 @@ export class ItemsApiService implements ItemsServiceInterface {
4746
return of(result);
4847
};
4948
}
49+
5050
}

0 commit comments

Comments
 (0)