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:
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."
– Augusto Vasques
I’m sorry, could be more clear, I didn’t quite understand
– Sanches
Take a look at the link I gave you. There’s saying that you have to create an object
options
with the propertyroot
set to the directory that will be root for relative paths. Use this objectoptions
as the second parameter tores.sendFile()
– Augusto Vasques