Grunt Connect proxy server: 404 not found

I am using grunt-connect-proxy "^ 0.2.0" for the proxy server for api from my angularjs application. The project was started with a yoman angular generator.

I followed the instructions here , but when using a proxy server, I get:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9000/api/users 

My proxy configuration:

  connect: { options: { port: 9000, // Change this to '0.0.0.0' to access the server from outside. hostname: 'localhost', livereload: 35729 }, proxies: [{ context: '/api', // the context of the data service host: 'localhost', // wherever the data service is running port: 8080, // the port that the data service is running on https: false, xforward: false }], 

My middleware:

 livereload: { options: { open: true, base: '', middleware: function (connect, options) { var middlewares = []; middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest); middlewares.push(connect.static('.tmp')); middlewares.push(connect.static('test')); middlewares.push(connect().use( '/bower_components', connect.static('./bower_components') )); middlewares.push(connect.static(appConfig.app)); return middlewares; } } }, 

My task

 grunt.registerTask('serve', 'Compile then start a connect web server', function (target) { if (target === 'dist') { return grunt.task.run(['build', 'connect:dist:keepalive']); } grunt.task.run([ 'clean:server', 'wiredep', 'concurrent:server', 'autoprefixer:server', 'configureProxies:server', 'connect:livereload', 'watch' ]); }); 

EDIT localhost: 8080 / users return 403 currently through Postman, so the API works.

+8
angularjs proxy gruntjs grunt-connect-proxy
source share
2 answers

not directly answering your question, but providing a solution (since no one answered) ... you tried the npm connect-modrewrite module

it does exactly what you are trying to do.

0
source share

I had the same problem as me and her work for me. Take a look at my code below: -

 connect: { server: { options: { keepalive: true, port: 8001, protocol: 'http', hostname: '*', directory: 'dist', open: { target: 'http://localhost:8001/myDemo.html', }, middleware: function(connect, options, middlewares) { middlewares.unshift(function(req, res, next) { res.setHeader('Access-Control-Allow-Credentials', true); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); **if (req.method.toUpperCase() == 'POST') req.method='GET';** return next(); }); return middlewares; } } } }, 

see the line marked with an asterisk, i.e. if (req.method.toUpperCase () == 'POST') req.method = 'GET'; I did this trick and it worked for me. This arch also helps me https://github.com/gruntjs/grunt-contrib-connect#middleware

0
source share

All Articles