NodeJS基本应用程序 - 404错误

时间:2018-03-08 07:57:22

标签: node.js express mongoose

我正在学习这个基础教程

https://www.joshmorony.com/creating-role-based-authentication-with-passport-in-ionic-2-part-1/

我的问题是,当浏览器进入:

http://localhost:8080/
http://localhost:8080/login
http://localhost:8080/register

我在终端上得到以下信息:

App listening on port 8080
GET / 404 5.436 ms - 139
GET /login 404 0.469 ms - 144
GET /register 404 0.451 ms - 147

我不知道为什么因为路由在文件上配置了app/routes.js,如下所示:

var AuthenticationController = require('./controllers/authentication'),
    TodoController = require('./controllers/todos'),
    express = require('express'),
    passportService = require('../config/passport'),
    passport = require('passport');

var requireAuth = passport.authenticate('jwt', { session: false }),
    requireLogin = passport.authenticate('local', { session: false });

module.exports = function (app) {

    var apiRoutes = express.Router(),
        authRoutes = express.Router(),
        todoRoutes = express.Router();

    // Auth Routes
    apiRoutes.use('/auth', authRoutes);

    authRoutes.post('/register', AuthenticationController.register);
    authRoutes.post('/login', requireLogin, AuthenticationController.login);

    authRoutes.get('/protected', requireAuth, function (req, res) {
        res.send({ content: 'Success' });
    });

    // Todo Routes
    apiRoutes.use('/todos', todoRoutes);

    todoRoutes.get('/', requireAuth, AuthenticationController.roleAuthorization(['reader', 'creator', 'editor']), TodoController.getTodos);
    todoRoutes.post('/', requireAuth, AuthenticationController.roleAuthorization(['creator', 'editor']), TodoController.createTodo);
    todoRoutes.delete('/:todo_id', requireAuth, AuthenticationController.roleAuthorization(['editor']), TodoController.deleteTodo);

    // Set up routes
    app.use('/api', apiRoutes);

}
  

如果您想自己尝试,这里是包含安装说明的存储库:

https://github.com/zeuscronos/node-ionic-01

关于如何解决这个问题的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:0)

使用以下代码段替换您的代码

var AuthenticationController = require('./controllers/authentication'),
    TodoController = require('./controllers/todos'),
    express = require('express'),
    passportService = require('../config/passport'),
    passport = require('passport');

var requireAuth = passport.authenticate('jwt', { session: false }),
requireLogin = passport.authenticate('local', { session: false });

module.exports = function (app) {

var apiRoutes = express.Router(),
    authRoutes = express.Router(),
    todoRoutes = express.Router();

// Auth Routes
authRoutes.post('/register', AuthenticationController.register);
authRoutes.post('/login', requireLogin, AuthenticationController.login);

authRoutes.get('/protected', requireAuth, function (req, res) {
    res.send({ content: 'Success' });
});

apiRoutes.use('/auth', authRoutes);


// Todo Routes

todoRoutes.get('/', requireAuth, AuthenticationController.roleAuthorization(['reader', 'creator', 'editor']), TodoController.getTodos);
todoRoutes.post('/', requireAuth, AuthenticationController.roleAuthorization(['creator', 'editor']), TodoController.createTodo);
todoRoutes.delete('/:todo_id', requireAuth, AuthenticationController.roleAuthorization(['editor']), TodoController.deleteTodo);
apiRoutes.use('/todos', todoRoutes);

// Set up routes
app.use('/api', apiRoutes);
}

您在添加路径路径之前已注册路线。这就是他们没有按预期工作的原因。现在您可以访问路径

API : /api
AUTH : /api/auth/login || /register
TODOS : /api/todos

答案 1 :(得分:0)

您构建应用的方式并非标准。您应该在开始使用它们之前定义路由器和路由(在您定义了要监听的请求后拨打app.use())。

如果您有不同的路由器,请将它们放在不同的文件中。

这就是事情不起作用的原因:

http://localhost:8080/:您没有任何索引路径

http://localhost:8080/loginhttp://localhost:8080/register:您已将这些路由注册为POST路由,因此GET请求无法正常工作,其次它们都有/auth的基本网址,因此您只能这样做。 ;能够使用它们是访问/auth/login/auth/register

定义两个额外的请求侦听器

authRoutes.get("/login"...)
authRoutes.get("/register"...)