Reputation: 1381
Folder structure:-
myapp
--public(directory) //here files which related to public(Angularjs,css,etc)
--server(directory) //here files which related to server
--server.js
Code
server.js //at root directory
----------------------
...
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public')); // set the static files location
app.use('/profilepic', express.static(__dirname + '/uploads/profilepic')); // set the static files location
app.use('/company', express.static(__dirname + '/uploads/company'));
app.use('/download', express.static(__dirname + '/uploads/download'));
app.use(morgan('dev'));
app.use(cookieParser());
app.use(expressSession({
secret: 'secret',
resave: true,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
require('./server/config/passport')(app, passport);
require('./server/route')(app, passport);
app.use(flash());
if (config.env === 'development') {
app.use(function(err, req, res, next) {
console.log(err);
return res.status(500).json({ message: err.message });
});
}
app.use(function(err, req, res, next) {
return res.status(500).json({ message: err.message });
});
app.listen(config.port, function() {
console.log('app listen on ' + config.port);
});
Gruntfile.js //at root directory
-----------------
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
options: {
reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
},
// when this task is run, lint the Gruntfile and all js files in src
build: ['Grunfile.js', './']
},
ngAnnotate: {
options: {
singleQuotes: true
},
app: {
files: {
'./dist/min-safe/services.js': ['./public/scripts/services/*.js'],
'./dist/min-safe/filters.js': ['./public/scripts/filters/*.js'],
'./dist/min-safe/directives.js': ['./public/scripts/directives/*.js'],
'./dist/min-safe/controllers.js': ['./public/scripts/controllers/*.js'],
'./dist/min-safe/app.ctrl.js': ['./public/scripts/app.ctrl.js'],
'./dist/min-safe/config.router.js': ['./public/scripts/config.router.js'],
'./dist/min-safe/config.lazyload.js': ['./public/scripts/config.lazyload.js'],
'./dist/min-safe/config.js': ['./public/scripts/config.js'],
'./dist/min-safe/app.js': ['./public/scripts/app.js']
}
}
},
concat: {
js: { //target
src: ['./dist/min-safe/app.js', './dist/min-safe/config.js',
'./dist/min-safe/config.lazyload.js', './dist/min-safe/config.router.js',
'./dist/min-safe/app.ctrl.js', './dist/min-safe/controllers.js',
'./dist/min-safe/directives.js', './dist/min-safe/filters.js', './dist/min-safe/services.js'
],
dest: './dist/min/lms.js'
}
},
copy: {
main: {
expand: true,
src: './dist/min/lms.js',
dest: './public',
flatten: true,
filter: 'isFile'
}
},
connect: {
options: {
base: 'xxxxxxxx',
keepalive: true
}
},
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-ng-annotate');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
// Default task(s).
grunt.registerTask('default', ['ngAnnotate', 'concat', 'copy']);
grunt.registerTask('server', ['default', 'connect']);
grunt.registerTask('jshint', ['jshint']);
};
I run app by node server.js
but now i want to run this app by grunt and when i run grunt server
the it throw No "connect" targets found error.
How to configure grunt file to run app?
Upvotes: 3
Views: 3842
Reputation: 24982
When i run grunt server the it throw No "connect" targets found error. How to configure grunt file to run app?
In your Gruntfile.js
update the connect
Task to include a Target as follows:
// ...
connect: {
server: { // <-- This is a Target named 'server'.
options: {
// <-- Your connect options go here.
// https://github.com/gruntjs/grunt-contrib-connect#options
}
}
}
Next update your grunt.registered.Task()
named server
to this:
grunt.registerTask('server', ['default', 'connect:server']);
Note: Instead of aliasing the connect
Task in the taskList
Array
it now references the server
Target in the connect
Task using the syntax 'connect:server'
See Creating Tasks for further information regarding grunt.registered.Task()
.
EDIT
Updated based on feedback in the comments and edit made to original post/question:
Assuming that running $ node server.js
via your CLI works as expected you can run the same command via grunt.
grunt-contrib-connect
by running:$ npm un -D grunt-contrib-connect
Delete line of code reading grunt.loadNpmTasks('grunt-contrib-connect');
from Gruntfile.js
Delete the connect
Task from Gruntfile.js
Run the following to install load-grunt-tasks and grunt-shell:
npm i -D load-grunt-tasks grunt-shell
Gruntfile.js
:// ...
shell: {
connect: {
command: 'node server.js'
}
}
// ...
grunt.registerTask()
to:grunt.registerTask('server', ['default', 'shell:connect']);
Gruntfile.js
:require('load-grunt-tasks')(grunt);
$ grunt server
and visit localhost:3000
Upvotes: 4