node js express routing [无法获取:/ collection#]

时间:2016-07-16 18:05:59

标签: javascript node.js express

我是nodejs的初学者。我有一个简单的api,用护照登录你,重定向到/ collections。该部分工作正常,问题出在POST请求中,返回

    Error: Not Found
   at D:\Movie API\src\server\server.js:44:15
   at Layer.handle [as handle_request] (D:\Movie API\src\server\node_modules\express\lib\router\layer.js:82:5)
   at trim_prefix (D:\Movie API\src\server\node_modules\express\lib\router\index.js:302:13)
   at D:\Movie API\src\server\node_modules\express\lib\router\index.js:270:7
   at Function.proto.process_params (D:\Movie API\src\server\node_modules\express\lib\router\index.js:321:12)
   at next (D:\Movie API\src\server\node_modules\express\lib\router\index.js:261:10)
   at D:\Movie API\src\server\node_modules\express\lib\router\index.js:603:15
   at next (D:\Movie API\src\server\node_modules\express\lib\router\index.js:246:14)
   at D:\Movie API\src\server\routes\secure.js:8:11
   at Layer.handle [as handle_request] (D:\Movie API\src\server\node_modules\express\lib\router\layer.js:82:5)
   at trim_prefix (D:\Movie API\src\server\node_modules\express\lib\router\index.js:302:13)
   at D:\Movie API\src\server\node_modules\express\lib\router\index.js:270:7
   at Function.proto.process_params (D:\Movie API\src\server\node_modules\express\lib\router\index.js:321:12)
   at next (D:\Movie API\src\server\node_modules\express\lib\router\index.js:261:10)
   at Function.proto.handle (D:\Movie API\src\server\node_modules\express\lib\router\index.js:166:3)
   at router (D:\Movie API\src\server\node_modules\express\lib\router\index.js:35:12)

withot错误:

Cannot GET /collection

我已经尝试过postman的sendig POST请求,这给了我一个404.

var auth = express.Router();

要求(' ./ routes / auth.js')(auth,护照); app.use(' / auth',auth);

服务器路由:

var auth = express.Router();
require('./routes/auth.js')(auth, passport);
app.use('/auth', auth);

var secure = express.Router();
require('./routes/secure.js')(secure);
app.use('/', secure);

auth路径工作正常。它接受带凭据的auth文件,以及使用auth凭证的护照。成功后,它会重定向到/ collections。 AUTH:

module.exports = function(router, passport){

router.get('/', function(req, res){
    res.render('index.ejs');
});

router.get('/google', passport.authenticate('google', {scope: ['profile', 'email']}));

router.get('/google/callback', 
  passport.authenticate('google', { successRedirect: '/collections',
                                      failureRedirect: '/' }));


router.get('/logout', function(req, res){
    req.logout();
    res.redirect('/');
})

router.get('/*', function(req, res){
    res.redirect('/auth');
})
};

现在这是安全的:

module.exports = function(router, passport){

router.use(function(req, res, next){
    if(req.isAuthenticated()){
        return next();
    }

    res.redirect('/auth');
})

// Get all collections
router.get('/collections', collection.getAll);

// Create a collection
router.post('/collection', collection.create);
// Get one product, update one product, delete one product
router.route('/collection/:id')
    .get(collection.read)
    .put(collection.update)
    .delete(collection.delete);


}

收集模块功能仅使用res.json({id:req.params.id});

进行回复

从/ auth到/ collections的重定向是可以的,但是在POST / collection上我得到了上面提到的错误。有没有人有类似问题?

2 个答案:

答案 0 :(得分:2)

尝试使用var collection = require('../models/collection'); ..代替。

这是因为require是您不在根目录中的文件的相对路径。

答案 1 :(得分:1)

var collection = require('../models/collection');

Source code link

Node.js require()使用CommonJS语法,所有路径都相对于调用它的文件。

请参阅docs