Home>

・ Problems
I want to make a login decision and jump to a different page, but I'm facing a problem where I can't redirect.


I want to move admin.js withres.redirect ('/admin');and display it.

This is part of the source code

part of app.js

var createError = require ('http-errors');
var express = require ('express');
var path = require ('path');
var cookieParser = require ('cookie-parser');
var logger = require ('morgan');
var session = require ('express-session');
var usersRouter = require ('./ routes/users');
var login = require ('./ routes/login');
var admin = require ('./ routes/admin');

var app = express ();
// view engine setup
app.set ('views', path.join (__ dirname, 'views'));
app.set ('view engine', 'ejs');
app.use (logger ('dev'));
app.use (express.json ());
app.use (express.urlencoded ({extended: false}));
app.use (cookieParser ());
app.use (express.static (path.join (__ dirname, 'public')));
app.use (session ({
  secret: 'keyboard cat',
  resave: false,
  seveUniitialized: true
}));
app.use ('/', login);
app.use ('/ users', usersRouter);
app.use ('/ admin', admin);

login.js

var express = require ('express');
var router = express.Router ();
var sqlite3 = require ('sqlite3');
// get database object
var db = new sqlite3.Database ('ChieproDB.sqlite3');
/ * GET home pa0ge. * /
router.get ('/', function (req, res, next) {
  res.render ('login', {
    title: 'ChiePro Management System',
    message: 'Connect to login package'
   });
});
router.post ('/', function (req, res, nxet) {
  var id = req.body.id;
  var pass = req.body.pass;
  var query = `select * from account where admin_id =" ${id} "AND admin_pass =" ${pass} "`;
  db.all (query, function (err, rows) {
    if (rows.length) {
      console.log ('match');
      res.redirect ('/ admin');// admin is skipped here. * Error occurred Not Found 404
    } else {
      res.render ('login', {title: 'ChiePro Management System',
        message: 'ID or password is wrong. '
      });
    }
  });
});
module.exports = router;

Jes js
where I want to skip "Admin.js" in the file name routes

var express = require ('express');
var router = express.Router ();
/ * GET users listing. * /
router.get ('/ admin', function (req, res, next) {
  res.renber ('admin', {
      title: 'Admin screen',
      message: 'Success'
  });
});
module.exports = router;


"Admin.ejs" in the file name views

<! DOCTYPE html>
<html>
  <head>
    <title>Admin screen</title>
    <link rel = 'stylesheet' href = '/ stylesheets/style.css' />
    <!-bootstrap4->
    <link rel = "stylesheet" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMOony">;
    <script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity = "sha384-q8i/X + 965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH + 8abtTE1Pi6jizo" crossorigin = "anonymous;gt&gt/script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity = "sha384-ZMP7rVo3mIykV + 2 + 9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l849 = "anonymous"></script>
    <script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity = "sha384-ChfqqxuZUCnJSK3 + MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin = "&/ script>
  </head>
  <body>
    <div>
      <h1><% = title%></h1>
      <h6><% = message%></h6>
    </div>
  </body>
</html>

error code

Not Found
404
NotFoundError: Not Found
    at C: \ Users \ ******* \ Desktop \ atm \ app.js: 36: 8
    at Layer.handle [as handle_request] (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ layer.js: 95: 5)
    at trim_prefix (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 317: 13)
    at C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 284: 7
    at Function.process_params (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 335: 12)
    at next (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 275: 10)
    at C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 635: 15
    at next (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 260: 14)
    at Function.handle (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 174: 3)
    at router (C: \ Users \ ******* \ Desktop \ atm \ node_modules \ express \ lib \ router \ index.js: 47: 12)

]

  • Answer # 1

    The description in admin.js

    res.renber ('admin', {
          title: 'Admin screen',
          message: 'Success'
      });


    Is correct, but

    res.render ('admin', {
          title: 'Admin screen',
          message: 'Success'
      });


    is.