express middleware redirects not working

I have the next express server i want redirect .js files to the correspond .js.gz file but when i request /static/common.js don't redirects

Response Headers

HTTP/1.1 200 OK X-Powered-By: Express Accept-Ranges: bytes Cache-Control: public, max-age=0 Last-Modified: Wed, 29 Mar 2017 14:30:17 GMT ETag: W/"2b4084-15b1a796ca8" Content-Type: application/javascript Content-Length: 2834564 Date: Wed, 29 Mar 2017 16:25:53 GMT Connection: keep-alive

// define the folder that will be used for static assets
const staticFolder = './';
app.use(favicon(`${staticFolder}/icons/favicon.ico`));
app.use('/static', Express.static(staticFolder));
app.use('/', Express.static(`${staticFolder}/sw`));
app.use(locale(supported));

app.get('*.js', (req, res, next) => {
  const reqI = req;
  reqI.url += '.gz';
  res.set('Content-Encoding', 'gzip');
  next();
});

// universal routing and rendering
app.get('*', (req, res) => {

I am using nginx inside a docker image with the next configuration:

server {
    listen 80;
    server_name api.inmoblex.com;
    return 301 https://$host$request_uri;
}

server {
 listen 443 ssl http2;
 server_name api.inmoblex.com;
 charset utf-8;

 ssl_certificate /www/certs/fullchain.pem;
 ssl_certificate_key /www/certs/privkey.pem;

 location / {
     proxy_pass http://web:8000;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}

server {
    listen 80 default_server;
    server_name inmoblex.com www.inmoblex.com;
    return 301 https://$host$request_uri;
}

server {

   listen 443 ssl http2 default_server;
   server_name inmoblex.com www.inmoblex.com;
   charset utf-8;

   ssl_certificate /www/certs/fullchain.pem;
   ssl_certificate_key /www/certs/privkey.pem;

   location / {
       proxy_pass http://node:3000;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }

}

Upvotes: 3

Views: 544

Answers (2)

In this case my error is that the middleware never runs because .js file is served by static middleware.

I solved this issue using compression package before this not worked because i used this after static middleware.

import { Server } from 'http';
import Express from 'express';
import React from 'react';
import locale from 'locale';
import { sync as globSync } from 'glob';
import { renderToString } from 'react-dom/server';
import match from 'react-router/es6/match';
import RouterContext from 'react-router/es6/RouterContext';
import UAParser from 'ua-parser-js';
import { Provider } from 'react-redux';
import { Map } from 'immutable';
import favicon from 'serve-favicon';
import * as path from 'path';
import { readFileSync } from 'fs';
import { IntlProvider } from 'react-intl';
import routes from './routes';
import NotFoundPage from './components/NotFoundPage';
import configureStore from './stores';
import WithStylesContext from './WithStylesContext';

const compression = require('compression');
const supported = ['es', 'en'];


// initialize the server and configure support for ejs templates
const app = new Express();
const server = new Server(app);

// define the folder that will be used for static assets
const staticFolder = './';
app.use(compression());
app.use(favicon(`${staticFolder}/icons/favicon.ico`));
app.use('/static', Express.static(staticFolder));
app.use('/', Express.static(`${staticFolder}/sw`));
app.use(locale(supported));

Upvotes: 1

Jason Livesay
Jason Livesay

Reputation: 6377

Don't need to precompress or redirect.

var compression = require('compression')
var express = require('express')

var app = express()
app.use(compression())

Upvotes: 1

Related Questions