如何使用grunt-connect和livereload来测试上传文件到服务器?
我在我的项目中使用了Yeoman和angular-seed。其中一个要求是能够将文件上传到服务器。我希望能够保留当前设置中包含的所有内容,而不是为项目设置一些外部服务器。
此解决方案将中间件直接添加到Gruntfile.js中以进行grunt连接。
首先,它加载connect bodyParser,这使得解析上传的文件和表单元素变得更容易。
接下来,它在'/upload‘处设置一个端点。这将是开发和测试期间使用的路线。此中间件返回带有文件属性的基本响应。
最后,Yeoman自动配置的静态路由被附加到中间件列表中,该列表返回给grunt-connect。
发布于 2014-01-16 23:46:40
用于向grunt connect添加文件上载处理程序的配置属性。
livereload: {
options: {
open: true,
base: [ '.tmp', '<%= yeoman.app %>' ],
middleware: function (connect, options) {
var middlewares = [
connect().use(connect.bodyParser({ uploadDir: '.tmp' })),
connect().use('/upload', function(req, res, next) {
/*
console.log(req.files); // files properties
console.log(req.body); // form properties
*/
res.setHeader('Content-Type', 'application/json');
// response with basic file stats
res.end(JSON.stringify({
'size': req.files.file.size,
'path' : req.files.file.path,
'other' : null }));
})
];
// add the static paths in options.base
options.base.forEach(function (base) {
middlewares.push(connect.static(base));
});
return middlewares;
}
}
}https://stackoverflow.com/questions/21166392
复制相似问题