Problem with routes

Asked

Viewed 189 times

1

When trying to access konic.com.br the site is loaded normally, but when I enter another route that in the case of konic.com.br/contact it returns 404

app js.:

const express = require("express");
const app = express();

app.use(express.static('html'));

app.get('/', (req, res) => {
    res.sendFile(__dirname, 'html/index.html');
});
app.get('/contato', (req, res) => {
    res.sendFile (__dirname, 'html/contato.html');
    res.send("Deu certo");
});

app.listen(3002).on('listening', () => {
    console.log("Servidor rodando na porta 3002!");
});

module.exports = app;

/etc/Nginx/sites-available/default

server {

    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;


    root /var/www/konic.com.br/html;

    index index.html index.htm index.nginx-debian.html;

    server_name konic.com.br www.konic.com.br;
    ssl_certificate /etc/letsencrypt/live/konic.com.br/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/konic.com.br/privkey.pem;

    location / {
        proxy_pass http://konic.com.br:3002;
        include /etc/nginx/mime.types;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        try_files $uri $uri/ =404;
    }

}


server {
    listen 80;
    listen [::]:80;

    server_name lucasmrpires.com.br www.lucasmrpires.com.br;

    root /var/www/lucasmrpires.com.br/html;
    index index.html;

    location / {
        proxy_pass http://localhost:3003;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        try_files $uri $uri/ =404;
    }
}

server {
    if ($host = konic.com.br) {
        return 301 https://$host$request_uri;
    } 

    listen 80 default_server;
    listen [::]:80 default_server;

    server_name konic.com.br www.konic.com.br;
    return 404; 


}

1 answer

1


When you access konic.com.br, Nginx responds appropriately and sends your browser your javascript application that is at the root containing your routes. From there, all navigation is controlled by JS.

However, when you try to access any other URL, Nginx is prepared to respond 404 when a folder does not exist (in your example there is no folder called /contact; this is a route that was defined by JS).

Note that whoever is prepared to respond to the routes is the JS application from the root, so it is normal that any call other than the root returns 404. In this case, you will need to add a rule in Nginx to always redirect to the root, from where the application will be downloaded and the routes can be accessed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.