【问题标题】:MEAN - router.get method is not triggeringMEAN - router.get 方法未触发
【发布时间】:2019-04-01 08:58:05
【问题描述】:

我的要求很简单。如果配置尚未完成,我正在尝试加载可配置的angular component。我一直在尝试向服务器发送一个获取请求以找出一条记录已经存在 - 如果存在,它将重定向到其他 angular component 或保留当前的 ​​angular component 进行配置。

我通过以下方式为路由器配置了我的express 应用程序-

const admin_routes = require('./server/routes/admin');
const configure_routes = require('./server/routes/configure');

app.use(mongooseExpressErrorHandler);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use(express.static(path.join(__dirname, 'dist/product-bot')));
app.use(express.static(__dirname + '/server/images'));

app.use('/admin', admin_routes);
app.use('/configure', configure_routes);

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/products/index.html'));
});

app.post('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/products/index.html'));
});

这是路由器 -

const express       = require('express');;
const configureController = require('../controller/configure/configure');
const router        = express.Router();

router.get('/configure', configureController.check_super_admin_exists);

这是我的控制器代码 -

exports.check_super_admin_exists = function(req, res, next) {
  let find_super_admin_promise = userModel.find_user_by_role();

  find_super_admin_promise.then(function(result) {

    if (typeof result === 'undefined' || result === null) {
      res.redirect('/login');
    } else {
      next();
    }
  }).catch(function(error) {
    console.log(error);
    res.status(500);
    res.json('error');
  });
}

userModel.find_user_by_role() 工作正常,因为我已经在另一个功能中使用了模型代码。

但问题是 - 我看到我的路由器代码 (router.get('/configure', configureController.check_super_admin_exists)) 从未被执行。我一直在同一个项目中使用router.post() 方法,它工作正常。

你能帮我找出我的代码有什么问题吗?

提前致谢。

【问题讨论】:

    标签: angular express mongoose mean


    【解决方案1】:

    至于我的观察,我认为这是因为您在快速应用程序中初始化的路线。

    例子:

    在你index.js / server.js上,你已经指定了这一行:

    app.use('/configure', configure_routes);
    

    但是在你的设置你的 /configure 子路由的其他文件上,你已经指定了一个路由的名称与你的父路由的名称相同(app.use('/配置', configure_routes))

    router.get('/configure', configureController.check_super_admin_exists);
    

    有了这个,你可以通过以下方式访问配置路由:

    /configure/configure       
    
    or
    
    http://localhost:3000/configure/configure     // if you're running at port 3000
    

    要以/configurehttp://localhost:3000/configure 访问它,您需要将其他文件上的路由配置编辑为'/'

    router.get('/', configureController.check_super_admin_exists);
    
    // This way, it will follow the parent's name setup from index / server.js
    // app.use('/configure', configure_routes);
    

    Express 示例路由结构:

    STRUCTURE           ROUTES                             API URL
    
    /user        app.use('/user', userRoutes)            
       /         router.get('/', getUser);               GET     /user
       /         router.post('/', saveUser);             POST    /user
       /:id      router.put('/:id', updateUser);         PUT     /user/:id
       /:id      router.delete('/:id', deleteUser);      DELETE  /user/:id
    

    【讨论】:

    • 肯定是关于 express 4 或其他什么的最令人困惑的事情之一
    猜你喜欢
    • 2012-02-05
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2012-08-25
    • 2021-04-18
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多