node.js - Grunt Connect , Nodemon same port issue -


i trying run angular admin template , node server grunt , nodemon.all runs well..problem when try send request client-side server-side showing 404 error. both using same port 2300. if change port number of server.js returns result..i can not run them same port. sending request http://localhost:2300/test

here gruntfile :

// generated on 2015-01-21 using generator-angular 0.9.2  'use strict';    // # globbing  // performance reasons we're matching 1 level down:  // 'test/spec/{,*/}*.js'  // use if want recursively match subfolders:  // 'test/spec/**/*.js'    module.exports = function (grunt) {      // load grunt tasks automatically    require('load-grunt-tasks')(grunt);          // time how long tasks take. can when optimizing build times    require('time-grunt')(grunt);      // configurable paths application    var appconfig = {      app: require('./bower.json').apppath || 'app',      dist: 'dist'    };      // define configuration tasks    grunt.initconfig({        // project settings      yeoman: appconfig,           // watches files changes , runs tasks based on changed files      watch: {        bower: {          files: ['bower.json'],          tasks: ['wiredep']        },        js: {          files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],          tasks: ['newer:jshint:all'],          options: {            livereload: '<%= connect.options.livereload %>'          }        },        jstest: {          files: ['test/spec/{,*/}*.js'],          tasks: ['newer:jshint:test', 'karma']        },        styles: {          files: ['<%= yeoman.app %>/styles/{,*/}*.css'],          tasks: ['newer:copy:styles', 'autoprefixer']        },        gruntfile: {          files: ['gruntfile.js']        },        livereload: {          options: {            livereload: '<%= connect.options.livereload %>'          },          files: [            '<%= yeoman.app %>/{,*/}*.html',            '.tmp/styles/{,*/}*.css',            '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'          ]        }      },        // actual grunt server settings      connect: {        options: {          port: 2300,          // change '0.0.0.0' access server outside.          hostname: 'localhost',          livereload: 35729        },        livereload: {          options: {            open: true,            middleware: function (connect) {              return [                connect.static('.tmp'),                connect().use(                  '/bower_components',                  connect.static('./bower_components')                ),                connect.static(appconfig.app)              ];            }          }        },        test: {          options: {            port: 9001,            middleware: function (connect) {              return [                connect.static('.tmp'),                connect.static('test'),                connect().use(                  '/bower_components',                  connect.static('./bower_components')                ),                connect.static(appconfig.app)              ];            }          }        },        dist: {          options: {            open: true,            base: '<%= yeoman.dist %>'          }        }      },        // make sure code styles par , there no obvious mistakes      jshint: {        options: {          jshintrc: '.jshintrc',          reporter: require('jshint-stylish')        },        all: {          src: [            'gruntfile.js',            '<%= yeoman.app %>/scripts/{,*/}*.js'          ]        },        test: {          options: {            jshintrc: 'test/.jshintrc'          },          src: ['test/spec/{,*/}*.js']        }      },        // empties folders start fresh      clean: {        dist: {          files: [{            dot: true,            src: [              '.tmp',              '<%= yeoman.dist %>/{,*/}*',              '!<%= yeoman.dist %>/.git*'            ]          }]        },        server: '.tmp'      },        // add vendor prefixed styles      autoprefixer: {        options: {          browsers: ['last 1 version']        },        dist: {          files: [{            expand: true,            cwd: '.tmp/styles/',            src: '{,*/}*.css',            dest: '.tmp/styles/'          }]        }      },        // renames files browser caching purposes      filerev: {        dist: {          src: [            '<%= yeoman.dist %>/scripts/{,*/}*.js',            '<%= yeoman.dist %>/styles/{,*/}*.css',            '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',            '<%= yeoman.dist %>/styles/fonts/*'          ]        }      },        // reads html usemin blocks enable smart builds automatically      // concat, minify , revision files. creates configurations in memory      // additional tasks can operate on them      useminprepare: {        html: '<%= yeoman.app %>/index.html',        options: {          dest: '<%= yeoman.dist %>',          flow: {            html: {              steps: {                js: ['concat', 'uglifyjs'],                css: ['cssmin']              },              post: {}            }          }        }      },        // performs rewrites based on filerev , useminprepare configuration      usemin: {        html: ['<%= yeoman.dist %>/{,*/}*.html'],        css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],        options: {          assetsdirs: ['<%= yeoman.dist %>','<%= yeoman.dist %>/images']        }      },        // following *-min tasks produce minified files in dist folder      // default, `index.html`'s <!-- usemin block --> take care of      // minification. these next options pre-configured if not wish      // use usemin blocks.      cssmin: {        dist: {          files: [{            expand: true,            cwd: '<%= yeoman.dist %>',            src: 'styles/*.css',            dest: '<%= yeoman.dist %>'          }]        }      },      uglify: {        build: {          files: [{              expand: true,              src: '**/*.js',              dest: '<%= yeoman.dist %>/scripts',              cwd: '<%= yeoman.app %>/scripts'          }]        },        options: {          mangle:false        },      },      // concat: {      //   dist: {}      // },        imagemin: {        dist: {          files: [{            expand: true,            cwd: '<%= yeoman.app %>/images',            src: '{,*/}*.{png,jpg,jpeg,gif}',            dest: '<%= yeoman.dist %>/images'          }]        }      },        svgmin: {        dist: {          files: [{            expand: true,            cwd: '<%= yeoman.app %>/images',            src: '{,*/}*.svg',            dest: '<%= yeoman.dist %>/images'          }]        }      },        htmlmin: {        dist: {          options: {            collapsewhitespace: true,            conservativecollapse: true,            collapsebooleanattributes: true,            removecommentsfromcdata: true,            removeoptionaltags: true          },          files: [{            expand: true,            cwd: '<%= yeoman.dist %>',            src: '**/*.html',            dest: '<%= yeoman.dist %>'          }]        }      },        // ngannotate tries make code safe minification automatically      // using angular long form dependency injection. doesn't work on      // things resolve or inject have done manually.      ngannotate: {        dist: {          files: [{            expand: true,           cwd: '<%= yeoman.app %>/scripts',            src: '**/*.js',           dest: '<%= yeoman.dist %>/scripts',          }]        }      },        // replace google cdn references      cdnify: {        dist: {          html: ['<%= yeoman.dist %>/*.html']        }      },        // copies remaining files places other tasks can use      copy: {        dist: {          files: [{            expand: true,            dot: true,            cwd: '<%= yeoman.app %>',            dest: '<%= yeoman.dist %>',            src: [ '**']          },{              expand:true,            cwd:'bower_components',            dest:'<%= yeoman.dist %>/bower_components',            src:['**']          } ,            {            expand: true,            cwd: '.tmp/images',            dest: '<%= yeoman.dist %>/images',            src: ['generated/*']          }, {            expand: true,            cwd: 'bower_components/bootstrap/dist',            src: 'fonts/*',            dest: '<%= yeoman.dist %>'          }]        },        styles: {          expand: true,          cwd: '<%= yeoman.app %>/styles',          dest: '.tmp/styles/',          src: '{,*/}*.css'        }      },        // watch our node server changes      nodemon: {        dev: {          script: 'server.js',          options: {            env: {              port: process.env.port || 2300            }                     }        }      },          // run tasks in parallel speed build process      concurrent: {        options: {          logconcurrentoutput: true        },        server: [          'copy:styles        ],        test: [          'copy:styles'        ],        dist: [          'copy:styles',          'imagemin',          'svgmin'        ]      },        // test settings      karma: {        unit: {          configfile: 'test/karma.conf.js',          singlerun: true        }      }    });      grunt.loadnpmtasks('grunt-nodemon');      grunt.registertask('serve', 'compile start connect web server', function (target) {      if (target === 'dist') {        return grunt.task.run(['build', 'connect:dist:keepalive']);      }        grunt.task.run([               'clean:server',        'concurrent:server',        'autoprefixer',        'connect:livereload',         'nodemon',                  'watch'      ]);    });       grunt.registertask('server', 'deprecated task. use "serve" task instead', function (target) {      grunt.log.warn('the `server` task has been deprecated. use `grunt serve` start server.');      grunt.task.run(['serve:' + target]);    });      grunt.registertask('test', [      'clean:server',      'concurrent:test',      'concurrent:tasks',      'autoprefixer',      'connect:test',      'karma'    ]);      grunt.registertask('build', [      'clean:dist',      'concurrent:dist',      'copy:dist',      'cssmin',      'ngannotate',      'uglify',      'htmlmin',      'concurrent:tasks'    ]);      grunt.registertask('default', [      'newer:jshint',      'test',      'build',         'concurrent:tasks'    ]);  };

here node server.js file :

'use strict';  var express = require("express");  var app 	= express();  var mongoose = require('mongoose');  var config  = require("./config");    var user = require('./api/auth/auth.model');  mongoose.connect(config.mongouri).connection;    var port = 2300;  var bodyparser = require("body-parser");  //var http = require('http').server(app);  //app.use(express.static(__dirname +'app'));  app.use(bodyparser.urlencoded({      extended: true  }));   app.use(bodyparser.json());       app.get('/test', function(req, res, next) {    res.json({user:'working'});  });  require('./routes')(app,passport);    app.listen(port,function(){  	console.log("server running on port - "+ port);    });

here output :

enter image description here

enter image description here

you shouldn't use connect (as going run web server you), if you're using node. should better go grunt-nodemon

take this, removed connect , applied grunt nodemon (advance usage solution), , worked fine (keeping other mins, sass, etc tasks before).

https://github.com/chriswren/grunt-nodemon#advanced-usage

concurrent: {   dev: {     tasks: ['nodemon', 'node-inspector', 'watch'],     options: {       logconcurrentoutput: true     }   } }, nodemon: {   dev: {     script: 'index.js',     options: {       nodeargs: ['--debug'],       env: {         port: '5455'       },       // omit property if aren't serving html files ,        // don't want open browser tab on start       callback: function (nodemon) {         nodemon.on('log', function (event) {           console.log(event.colour);         });          // opens browser on initial server start         nodemon.on('config:update', function () {           // delay before server listens on port           settimeout(function() {             require('open')('http://localhost:5455');           }, 1000);         });          // refreshes browser when server reboots         nodemon.on('restart', function () {           // delay before server listens on port           settimeout(function() {             require('fs').writefilesync('.rebooted', 'rebooted');           }, 1000);         });       }     }   } }, watch: {   server: {     files: ['.rebooted'],     options: {       livereload: true     }   }  } 

Comments