【问题标题】:Sails.js Viewing information from APISails.js 从 API 查看信息
【发布时间】:2019-01-23 21:49:53
【问题描述】:

我刚开始对 Sails.JS 感兴趣 我正在关注 Traversy Media 的教育指南。在本指南中,一切对我来说都很顺利,直到他生成一个 API 并通过localhost:1337/articles 访问它。 我无法访问这个文件,只有一个 404 页面,所以我在互联网上搜索寻求帮助。我发现我需要将/articles 添加到config/routes.js,但我还没有弄清楚在action 部分中放置什么。 在 Traversy Sails.JS 教程中的10:36,它显示了我无法浏览的部分。 有人能帮忙吗?提前致谢。

ArticlesController.js 文件是默认的

我的routes.js

    module.exports.routes = {


      'GET /':                   { action: 'view-homepage-or-redirect' },
      'GET /welcome':            { action: 'dashboard/view-welcome' },
      'GET /articles':           { action: 'view-Articles' },

      'GET /faq':                { view:   'pages/faq' },
      'GET /legal/terms':        { view:   'pages/legal/terms' },
      'GET /legal/privacy':      { view:   'pages/legal/privacy' },
      'GET /contact':            { view:   'pages/contact' },

      'GET /signup':             { action: 'entrance/view-signup' },
      'GET /email/confirm':      { action: 'entrance/confirm-email' },
      'GET /email/confirmed':    { view:   'pages/entrance/confirmed-email' },

      'GET /login':              { action: 'entrance/view-login' },
      'GET /password/forgot':    { action: 'entrance/view-forgot-password' },
      'GET /password/new':       { action: 'entrance/view-new-password' },

      'GET /account':            { action: 'account/view-account-overview' },
      'GET /account/password':   { action: 'account/view-edit-password' },
      'GET /account/profile':    { action: 'account/view-edit-profile' },


      '/api/v1/account/logout':                              { action: 'account/logout' },
      'PUT   /api/v1/account/update-password':               { action: 'account/update-password' },
      'PUT   /api/v1/account/update-profile':                { action: 'account/update-profile' },
      'PUT   /api/v1/account/update-billing-card':           { action: 'account/update-billing-card' },
      'PUT   /api/v1/entrance/login':                        { action: 'entrance/login' },
      'POST  /api/v1/entrance/signup':                       { action: 'entrance/signup' },
      'POST  /api/v1/entrance/send-password-recovery-email': { action: 'entrance/send-password-recovery-email' },
      'POST  /api/v1/entrance/update-password-and-login':    { action: 'entrance/update-password-and-login' },
      'POST  /api/v1/deliver-contact-form-message':          { action: 'deliver-contact-form-message' },


      '/terms':                   '/legal/terms',
      '/logout':                  '/api/v1/account/logout',

    };

Blueprints is also default

【问题讨论】:

  • 你有文章控制器吗?请发布它的代码,也请发布 config/blueprints.js 和 config/routes.js(最好不要 cmets)
  • 是的,我有一个 ArticlesController
  • 还请检查,您在风帆上使用的版本与视频中的版本相同。最新版本是1,但是视频是0.12左右
  • 刚刚也注意到了这一点。但我仍然对如何访问 API 感到困惑

标签: sails.js


【解决方案1】:

如果你在

config/blueprints.js

module.exports.blueprints = {
  actions: true,
  rest: false,
  shortcuts: true,
};

和 ArticleController 以及那里的一些动作(方法),例如

module.exports = {
  hello: function(req, res) {
    return res.json('ok');
  }
}

您应该能够通过 url localhost:1337/Article/hello 看到“ok”

【讨论】:

  • 仍然得到 404
  • 是的,它们都有效,只是文章无效,
  • 我使用 nodemon 检查是否有任何更改。我已经尝试过sails lift 和nodemon app.js
  • 如果你说其他路由没问题,那就意味着它是关于控制器或动作的东西
  • 我只需要能够看到数据库中的文章
猜你喜欢
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2022-08-09
  • 2019-12-12
  • 2018-04-10
  • 2016-12-11
相关资源
最近更新 更多