简体   繁体   中英

SailsJS - Route loading forever

I am a Junior laravel developer and I want to switch to NodeJS. I'm trying to build an API using SailsJS, and at first I want to create an instance of a model and return it as JSON string. So:

I created a new SailsJS app

$ sails new app
cd  Choose a template for your new Sails app:
1. Web App  ·  Extensible project with auth, login, & password recovery
2. Empty    ·  An empty Sails app, yours to configure
(type "?" for help, or <CTRL+C> to cancel)
1
 info: Installing dependencies...
Press CTRL+C to cancel.
(to skip this step in the future, use --fast)
info: Created a new Sails app `app`!

Then a created a new api (along with model and controller)

$ sails generate api school
info: Created a new api!

This is my config/routes.js file, I added the api/school route which redirects to getSchool function in SchoolController:

  module.exports.routes = {
  //  ╦ ╦╔═╗╔╗ ╔═╗╔═╗╔═╗╔═╗╔═╗
  //  ║║║║╣ ╠╩╗╠═╝╠═╣║ ╦║╣ ╚═╗
  //  ╚╩╝╚═╝╚═╝╩  ╩ ╩╚═╝╚═╝╚═╝
  'GET /':                   { action: 'view-homepage-or-redirect' },
  'GET /welcome/:unused?':   { action: 'dashboard/view-welcome' },

  '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' },
  'GET /api/school': 'SchoolController.getSchool',


  //  ╔╦╗╦╔═╗╔═╗  ╦═╗╔═╗╔╦╗╦╦═╗╔═╗╔═╗╔╦╗╔═╗   ┬   ╔╦╗╔═╗╦ ╦╔╗╔╦  ╔═╗╔═╗╔╦╗╔═╗
  //  ║║║║╚═╗║    ╠╦╝║╣  ║║║╠╦╝║╣ ║   ║ ╚═╗  ┌┼─   ║║║ ║║║║║║║║  ║ ║╠═╣ ║║╚═╗
  //  ╩ ╩╩╚═╝╚═╝  ╩╚═╚═╝═╩╝╩╩╚═╚═╝╚═╝ ╩ ╚═╝  └┘   ═╩╝╚═╝╚╩╝╝╚╝╩═╝╚═╝╩ ╩═╩╝╚═╝
  '/terms':                   '/legal/terms',
  '/logout':                  '/api/v1/account/logout',

This is api/controllers/SchoolController.js, for testing purposes I'm just returning the string 'School 123'

module.exports = {
    getSchool: function() {
        return 'School 123';
    },
};

I want it to work without requiring login, so I modified config/policies.js

module.exports.policies = {
  '*': 'is-logged-in',
  // Bypass the `is-logged-in` policy for:
  'entrance/*': true,
  'account/logout': true,
  'view-homepage-or-redirect': true,
  'deliver-contact-form-message': true,
  SchoolController: {
    'getSchool': true
  },
};

The problem now is that when I'm trying to visit http://localhost:1337/api/school I am expecting the string 'School 123' but instead the browser loads forever, same with Postman. What am I doing wrong?

Thanks in advance!

It's taking forever because you are not returning any response, and postman is waiting for some kind of response. Check out this link

module.exports = {
    getSchool: function(req, res) {
        return res.send('School 123');
    },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM