I want to do the user authentication and return a page after the user clicks the login button, I start the project by using express-generator and write the following code in index.js and login.js:
我想进行用户身份验证并在用户单击login按钮后返回一个页面,我使用express-generator启动项目,并在index中编写以下代码。js和login.js:
index.js:
index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
module.exports = router;
login.js:
login.js:
var express = require('express');
var router = express.Router();
/* GET main page after user have logged in. */
router.get("/login",function(req, res, next) {
res.render('home');
});
module.exports = router;
p.s index and home are the pug file in views folder
p。s索引和home是视图文件夹中的pug文件
It is my app.js
这是我app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/login', login);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
When I type npm start, the index.js works and show the page correctly, but when I type localhost:3000/login, it shows 404 not found, how can I solve this problem?
当我输入npm启动时,索引。js工作并正确显示页面,但是当我输入localhost:3000/login时,会显示404未找到,如何解决这个问题?
3 个解决方案
#1
3
This:
这样的:
app.use('/login', login)
Combined with this:
结合:
router.get("/login", ...)
Creates a route handler for the path /login/login
.
为路径/登录/登录创建路径处理程序。
Because you are mounting the login
router on /login
, the router itself should define its paths relative to that mountpoint, like this:
因为您正在/login上安装登录路由器,路由器本身应该定义它相对于该挂载点的路径,如下所示:
router.get("/", ...)
#2
0
Your endpoint is actually at /login/login
. The route files append to the urls set in the app.
您的端点实际上是/login/login。路由文件附加到应用程序中的url。
var express = require('express');
var router = express.Router();
// `/` Is appended to `/login`
router.get("/",function(req, res, next) {
res.render('home');
});
module.exports = router;
#3
-1
make the below changes in login.js:
在login.js中进行以下更改:
router.get("/", function(req, res, next) {
res.render('home');
});
#1
3
This:
这样的:
app.use('/login', login)
Combined with this:
结合:
router.get("/login", ...)
Creates a route handler for the path /login/login
.
为路径/登录/登录创建路径处理程序。
Because you are mounting the login
router on /login
, the router itself should define its paths relative to that mountpoint, like this:
因为您正在/login上安装登录路由器,路由器本身应该定义它相对于该挂载点的路径,如下所示:
router.get("/", ...)
#2
0
Your endpoint is actually at /login/login
. The route files append to the urls set in the app.
您的端点实际上是/login/login。路由文件附加到应用程序中的url。
var express = require('express');
var router = express.Router();
// `/` Is appended to `/login`
router.get("/",function(req, res, next) {
res.render('home');
});
module.exports = router;
#3
-1
make the below changes in login.js:
在login.js中进行以下更改:
router.get("/", function(req, res, next) {
res.render('home');
});