使用 grunt 运行服务器时,出现 Cannot GET 错误/
server.js
var express = require('express');
var app = express();
var port = process.env.PORT || 2323;
app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendfile('./server/views/index.html');
});
app.listen(port, function(){
console.log('server is running on ' + port);
});
Gruntfile.js
module.exports = function(grunt){
var modRewrite = require('connect-modrewrite');
var serveStatic = require('serve-static');
grunt.initConfig({
connect: {
server: {
options: {
port: 2323,
base: 'views',
keepalive: true,
livereload: 35729,
hostname: 'localhost',
open: true,
middleware: function (connect) {
return [
modRewrite(['^[^\\.]*$ /index.html [L]']),
connect().use('/', serveStatic('/views'))
];
}
}
}
}
// configure connect
});
//load nodemon
grunt.loadNpmTasks('grunt-contrib-connect');
//register the nodemon task when we run grunt
grunt.registerTask('default', ['connect:server']);
};
而且我不知道我的 grunt 文件代码是对还是错 实际上是新的 grunt ,我不知道该怎么办,在运行 grunt 时遇到无法获取/此错误。
最佳答案
您只是在设置 Grunt 中运行的连接服务器时出现错误:
connect().use('/', serveStatic('/views'))
应该是
connect().use('/', serveStatic('./server/views'))
关于angularjs - 使用 Grunt Connect 时无法获取/获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649121/