R
R
Roman Andreevich2019-05-08 11:05:18
Apache HTTP Server
Roman Andreevich, 2019-05-08 11:05:18

Why does a request without a slash at the end of the URL return a redirect to a URL with a slash?

Colleagues, good day, the essence of the problem is this, a standard site on nodejs + express, everything is just the main file:

const path = require('path');
const http = require('http');
const express = require('express');
const helmet = require('helmet');
const minify = require('express-minify-html');
const session = require('express-session');
const favicon = require('express-favicon');
const MySQLStore = require('connect-mysql')(session);
const bodyParser = require("body-parser");
const compression = require('compression');
const config = require('./config.json');
const router = require('./controllers/router');

const app = express();
app.use(helmet());

app.use(session({
    cookie: {
        path: '/',
        httpOnly: true,
        maxAge: 24 * 60 * 60 * 1000
    },
    store: new MySQLStore({
        config: {
            user: config.db.user,
            password: config.db.password,
            database: config.db.name
        }
    }),
    key: config.session.key,
    secret: config.session.secret,
    resave: true,
    saveUninitialized: false
}));

app.use(compression());
app.use(minify({

    override:      true,
    exception_url: false,
    htmlMinifier: {

        removeComments:            true,
        collapseWhitespace:        true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes:     true,
        removeEmptyAttributes:     true,
        minifyJS:                  true,
        minifyCSS:                  true

    }

}));

app.use(favicon(path.join(__dirname, '/favicon.ico')));
app.set('views', path.join(__dirname, 'dist'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/dist'));
app.use(router);

app.use((req, res, next) => {

  next(404);

});

app.use((err, req, res, next) => {

    res.status(err.status || 404);
    res.render('404/index.ejs');

});

const server = http.createServer(app);
server.listen(config.port, () => {

  	

});

and the actual router:
// router
'use strict';
const express = require('express');
const router = express.Router({ strict: true });

const home = require('./home');
router.get('/', home.get);

const contacts = require('./contacts');
router.get('/contacts', contacts.get);

const corporative = require('./corporative');
router.get('/corporative', corporative.get);

const authenticity = require('./authenticity');
router.get('/authenticity', authenticity.get);

module.exports = router;

In general, everything should be clear and not cause questions, but there is a small nuance, everything was always fine with this code))))) but I started a new project, and the browser now makes requests to page 2, here is a screen:
5cd28ce4d2f57957664417.png
What is the problem, tell me, broke his whole head. Moreover, my router only processes a request like /url/, and the second one does not even reach
Here is the Apache config, just in case:
<VirtualHost *:80>

  ServerAdmin [email protected]
  ServerName testsite.localhost
  ServerAlias www.testsite.localhost

  ProxyRequests on
  <Proxy *>
      Order deny,allow
      Allow from all
  </Proxy>
  <Location />
      ProxyPass  http://127.0.0.1:5263/
      ProxyPassReverse  http://127.0.0.1:5263/
  </Location>

  #RewriteEngine on
  #RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
  #RewriteRule . %1/&2 [R=301,L]

  #RewriteCond %{REQUEST_FILENAME} !d
  #RewriteCond %{REQUEST_URI} ^(.+)/$
  #RewriteRule ^(.+)/$ $1 [R=301,L]

  #RewriteCond %{SERVER_NAME} =testsite.online [OR]
  #RewriteCond %{SERVER_NAME} =www.testsite.online

</VirtualHost>

in my case, it acts as a proxy, and actually does nothing but redirect the request.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2019-05-08
@RidgeA

RTFM:
expressjs.com/en/4x/api.html#app.set - `strict routing`
expressjs.com/en/4x/api.html#express.router - `strict`

L
Lynn "Coffee Man", 2019-05-08
@Lynn

1. The 301 redirect is cached by the browser and cannot be trusted. Check with curl/wget. Check the Apache logs.
2. If the request did reach the server, check if it reached the node. If you didn’t get it, then dig the Apache, if you got it, dig the node.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question