Simple page app routes to same view or controller SailsJS

后端 未结 3 515
不知归路
不知归路 2021-01-13 07:55

How can I route multiple urls to the same controller or view to work with angular single page app?!

I can do this but i think is ugly..

\'/\': {
            


        
相关标签:
3条回答
  • 2021-01-13 08:35

    I don't know if your approach succeded but in my head mapping angular routes clientside to node routes serverside is misleading. My approach to an single page application with these two libraries would be:

    1. having the default sails view deliver your index and angular.js
    2. having the angular routes working your clientside routes together with controllers, etc
    3. managing the complete ui and page flow in angular
    4. using the default sails api's with angular ressource to work on the node models (dont forget to use promises)
    5. using the node / sails routes to add specials on top of that if needed (e.g. login management)
    6. using css framework to compile css (e.g. sass with compass or less)
    0 讨论(0)
  • 2021-01-13 08:36

    You can't currently use real regular expressions for routing. You can however use a wildcard route to do what you want (aim multiple routes at one view).

    Put this at the end of your /config/routes.js object:

    '/*': function(req, res, next) {
             if (req.path.match(/\..*/g)) {
                return next();
             } else {
                return res.view('homepage');
             }
          }
    

    and everything that isn't matched by one of the routes above it will execute that function. The function first checks if you're trying to access a static asset (something with a file extension, like .js or .css), and if so, continues matching the route so that the Express static middleware can server the file. Otherwise, it will server your view.

    Update

    As of Sails v0.10.0-rc5, regular expressions can be used to define routes. From the docs:

    The syntax for a regular expression route is:

    "r|<regular expression string>|<comma-delimited list of param names>"
    

    That's the letter "r", followed by a pipe, a regular expression string without delimiters, another pipe, and a list of parameter names that should be mapped to parenthesized groups in the regular expression. For example:

    "r|^/\d+/(\w+)/(\w+)$|foo,bar": "MessageController.myaction"
    

    will match /123/abc/def, running the myaction action of MessageController and supplying the values abc and def as req.param('foo') and req.param('bar'), respectively.

    0 讨论(0)
  • 2021-01-13 08:45

    You can also route them simultaneously using | operator

    '/' | '/login' | '/register' | '/troller': {
                             view: 'homepage'
    }
    
    0 讨论(0)
提交回复
热议问题