gulp-connect로 다른 디렉토리의 일부 파일을 제공 할 수 있는지 궁금합니다. 다음과 같은 것 :
http://localhost:8080/index.html => root: '/root/app'
그러나
http://localhost:8008/js/main.js => from '/root/js/' not from 'root/app/js'
http://localhost:8008/css/main.css => from '/root/css/' not from 'root/app/css/'
요청 객체를 수정하여 요청 URL을 다시 작성할 수 있는 미들웨어 함수 를에 전달할 수 있습니다 gulp-connect
.
gulp.task('serve', function() {
connect.server({
root: 'root',
middleware: function() {
return [ function(req, res, next) {
if (!/^\/(js|css)\/.*/.test(req.url)) {
req.url = '/app' + req.url;
}
next();
}];
}
});
});
위의 경로로 시작 /js/
하거나 /css/
변경되지 않고 통과 할 경로입니다 . 우리의 기본 폴더는 다음 root
과 같은 경로 /js/main.js
가 root/js/main.js
.
다른 모든 경로는으로 시작됩니다 /app
. 즉, 같은 경로 /index.html
가로 투명하게 확인됩니다 root/app/index.html
.
위에서했던 것처럼 커스텀 로직을 사용하는 대신, http-rewrite-middleware
nginx에서 영감을받은 재 작성 표현식을 지정할 수 있는 같은 것을 사용할 수도 있습니다 .
var rewrite = require('http-rewrite-middleware');
gulp.task('serve', function() {
connect.server({
root: 'root',
middleware: function() {
return [ rewrite.getMiddleware([
{ from: '^/js/(.*)$', to: '/js/$1' },
{ from: '^/css/(.*)$', to: '/css/$1' },
{ from: '^(.*)$', to: '/app/$1' }
])];
}
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다