这很简单:我想在将网站部署到Apache之前使用grunt-contrib-connect模拟我网站的页面大小。
使用Apache,开启gzip压缩非常简单。但是,使用grunt-contrib-connect,我还没有找到让它工作的方法。这是我到目前为止在Gruntfile.js中尝试的内容:
var compression = require('compression');
...
// The actual grunt server settings
connect: {
dev: {
options: {
open: false,
base: 'dev',
port: '4000',
hostname: 'localhost',
livereload: 35729,
onCreateServer: [compression()]
}
}
}
我也试过了:
connect: {
dev: {
options: {
open: false,
base: 'dev',
port: '4000',
hostname: 'localhost',
livereload: 35729,
middleware: [compression()]
}
}
}
我可以正确地提供文件,但是当我使用Chrome开发工具查看网络请求时,我可以看到没有对连接网络服务器提供的任何文件应用压缩。我做错了什么?
答案 0 :(得分:3)
在grunt-contrib-connect任务中,如果您向中间件提供Array,它将完全用您提供的内容替换默认中间件。但是,如果您将中间件选项指定为函数,它将按预期工作(将中间件链接到默认中间件
middleware: function(connect, options, middlewares) {
middlewares.unshift(compression());
return middlewares;
}
答案 1 :(得分:0)
不确定这是否会对任何人有所帮助,但我无法弄清楚上面只使用compress: {
dist: {
options: {
mode: 'gzip'
},
expand: true,
cwd: 'dist/',
src: ['**/*'],
dest: 'dist_compress/'
}
}
的内容。我假设这是一个能够动态压缩文件的简写。由于我没有找到任何看起来会告诉我如何操作的信息,我只是使用grunt-contrib-compress将我的所有文件gzip到一个新目录中。
Gruntfile条目:
connect
然后我为Content-Encoding
添加了一个新的服务器条目,其中包含一个小的中间件函数来更改connect: {
server_gzip: {
options: {
port: 9004,
livereload: false,
base: 'dist_compress',
middleware: function(connect, options, middlewares) {
middlewares.unshift(function(req, res, next) {
res.setHeader('Content-Encoding', 'gzip');
return next();
});
return middlewares;
},
}
}
}
标头。这使我能够在启用gzip的情况下进行本地性能测试。
Gruntfile条目:
AsyncTask<String,String,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx)
{
context =ctx;
}
@Override
protected String doInBackground(String... params) {
String type=params[0];
String login_url="192.168.10.9/login.php";
if(type.equals("login"))
{
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String Result) {
Log.i("ok", "Result " + Result);
alertDialog.setMessage(Result);
alertDialog.show();
}
请注意,需要关闭livereload,因为这会导致事情失败。