您好,我有一些架构,我的前端应用程序是使用 grunt 构建的,它在 localhost:3000 nodejs 服务器上运行,我的后端应用程序位于 Apache tomcat 服务器 (localhost:8080) 中。基本上在 spring 框架上运行的后端应用程序。伙计们,我想使用 gruntjs 从我的 localhost:3000 向 localhost:8080 发送请求。
请帮助我。
这是我的 grunt js 文件
module.exports = function(grunt){
grunt.initConfig({
concat: {
options: {
separator : '\n\n//--------------------------------------------------\n\n;',
banner : '\n\n//---------------All Js file is here ---------------\n\n'
},
dist :{
src :['components/scripts/*.js'],
dest:'builds/development/js/scripts.js'
}
},
sass : {
dist :{
options:{
style:'expanded'
},
files:[{
src:'components/sass/style.scss',
dest:'builds/development/css/style.css'
}]
}
},
connect:{
server:{
options:{
hostname:'localhost',
port:'3000',
base:'builds/development/',
livereload:true
}
}
},
watch: {
scripts: {
files: ['builds/development/**/*.html',
'components/scripts/**/*.js',
'components/sass/**/*.scss'],
tasks: ['concat','sass'],
options: {
spawn: false,
livereload:true
},
},
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('default',['concat','sass','connect','watch']);
};
最佳答案
要解决您的问题,您必须编写单独的 Node 服务器并注册 grunt 任务。
var http = require('http');
var httpProxy = require('http-proxy');
var server = http.createServer(function(){
httpProxy.createProxyServer({target: 'http://localhost:8080'}).web(req, res);
});
module.exports = function(grunt) {
grunt.registerTask('server', function() {
// this.async(); // run forever
server.listen(8000);// Front End Server Listening Port
});
};
关于node.js - 发送请求到 Apache Tomcat (localhost :8080) using GruntJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32887907/