Skip to content

Commit

Permalink
fix(api): 修正API拦截器中的URL处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Encaik committed Sep 29, 2024
1 parent fcfd73f commit 16c3b34
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions proxy.conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
'/api': {
// target: 'https://wanjie-api.vercel.app',
target: 'http://localhost:3000',
target: 'https://wanjie-api.vercel.app',
// target: 'http://localhost:3000',
secure: false,
changeOrigin: true,
logLevel: 'debug'
Expand Down
12 changes: 7 additions & 5 deletions src/app/interceptors/api-interceptor.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { HttpInterceptorFn } from '@angular/common/http';
import { environment } from 'environments/environment';

export const apiInterceptor: HttpInterceptorFn = (req, next) => {
if (req.url.startsWith('/api')) {
const newUrl = `${environment.apiUrl}${req.url}`;
const modifiedReq = req.clone({ url: newUrl });
return next(modifiedReq);
// 统一加上服务端前缀
let url = req.url;
if (!url.startsWith('https://') && !url.startsWith('http://')) {
const { apiUrl } = environment;
url = apiUrl + (apiUrl.endsWith('/') && url.startsWith('/') ? url.substring(1) : url);
}
return next(req);
const newReq = req.clone({ url });
return next(newReq);
};

1 comment on commit 16c3b34

@vercel
Copy link

@vercel vercel bot commented on 16c3b34 Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wanjie – ./

wanjie.vercel.app
wanjie-git-main-encaiks-projects.vercel.app
wanjie-encaiks-projects.vercel.app

Please sign in to comment.