How to change pages through nodejs?

Asked

Viewed 402 times

2

Well, I tried to switch pages hit the login button, but I get this error:

"path must be absolute or specify root to res.sendFile".

My folder structure:

inserir a descrição da imagem aqui

HTML form (index.html):

  <div class="bg-modal">
      <form action="/login" class="login-form" method="post">
        <h1>Entrar</h1>
        <div id="close" class="close">+</div>

        <div class="txtb">
          <input type="text">
          <span data-placeholder="RA"></span>
        </div>

        <div class="txtb">
          <input type="password">
          <span data-placeholder="Senha"></span>
        </div>

        <input type="submit" class="logbtn" value="Entrar">

        <div class="bottom-text">
          Não possuí conta? <a id="botaoRegistrar" href="#corpoForm">Crie sua Conta</a>
        </div>

      </form>
  </div>

js routes.:

//var AlunoDao = require('../app/aluno-dao');
var conexao = require('../config/custom-mssql');

module.exports = (app) => {
    app.get('/', (req, res) => {
        res.sendFile('dashboard.html');
    });
    app.post('/login', (req, res) => {
        console.log("FUNCIONOU");
        res.sendFile('index.html');
    });
}

custom-express.js:

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

const bodyParser = require('body-parser');
//const expressLayouts = require('express-ejs-layouts');

const rotas = require('../app/rotas.js');


// informar o express que vamos usar o EJS como engine de visualização
//app.set('view engine', 'ejs');

// informar o express que vamos usar os módulos relacionados às variaveis abaixo
//app.use(expressLayouts);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/views'));
app.use(express.static(__dirname + '/public'));
app.use(express.static('public')); 
app.use(express.static('views')); 
app.use(bodyParser.json());

//acrescentando informacoes de cabecalho para suportar o CORS
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PATCH, DELETE");
    next();
  });

// passar como parâmetro para o módulo rotas.js
rotas(app);

module.exports = app;

On entering localhost:3000 is loaded the index.html (works), but after pressing the button entrar he falls into the method app.post(...), but cannot render the dashboard.html.

I tried to replace the res.sendFile('index.html') for res.sendFile('dashboard.html'), and it continues to render the index.html. How is that possible?

  • In the documentation of Express:"Unless the root option is set in the options object, path must be an absolute path to the file."

  • I’m sorry, could be more clear, I didn’t quite understand

  • Take a look at the link I gave you. There’s saying that you have to create an object options with the property root set to the directory that will be root for relative paths. Use this object options as the second parameter to res.sendFile()

1 answer

3


If you do not understand what Augusto Vasques commented, solve this way:

In its route code for the post, pass an object as the second argument with the attribute root setting the absolute path to know which HTML file should be sent.

Would look like this:

app.post('/login', (req, res) => {
    console.log("FUNCIONOU");
    res.sendFile('dashboard.html', {
      root: path.join(__dirname , 'views')
    });
});    

Once done, the express will know which file and where this file is to be sent in the reply.

Browser other questions tagged

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