问题
I use the Express 4.12 to build my project and I want to use grunt-express to start my application. I have known that Express 4.12 start the application via node ./bin/www, so I write the Gruntfile.js like this:
express: {
dev: {
options: {
port: 3000,
bases: path.resolve('bin/'),
server: path.resolve('bin/www')
}
}
}
grunt.loadNpmTasks('grunt-express')
grunt.registerTask('default', ['express', 'express-keepalive']);
Unfortunately, when I run grunt, it reports an error:
Fatal error: Server should provide a function called "listen" that acts as http.Server.listen
But I found that in the www file, there is a function "listen":
var server = http.createServer(app)
server.listen(port);
I am confused about this. Could you please teach me write the Gruntfile.js for the www file?
回答1:
The grunt-express
server option expects to see an express app, which is not what bin/www
is. If you look at bin/www
, what is it requiring for an express app? Eg, it should be an app.js
in your project root, or maybe it would be server/index.js
.
Also, your bases
probably shouldn't be bin
, rather it should be pointing to a public static resources dir, eg /public
or if you have a build step that generates minified stuff it might be /dist
.
Try this (editing server and bases to match your project):
grunt.initConfig({
express: {
options: {
port: 3000,
hostname: 'localhost'
},
dev: {
options: {
server: path.resolve('./app.js'),
bases: [path.resolve('./public')]
}
}
}
});
来源:https://stackoverflow.com/questions/30559774/how-can-i-use-the-grunt-express-for-express-4-12