English 中文(简体)
使用快车道
原标题:Using routes in Express-js
最佳回答
问题回答

举例来说,ur路与 objects相匹配,而后者则与http verbs与功能相匹配。 这在树中铺设了一条路,简明易读。 这些信标实体也作为随附方法具有功能的物体书写。

var express = require( ../../lib/express )
  , verbose = process.env.NODE_ENV !=  test 
  , app = module.exports = express();

app.map = function(a, route){
  route = route ||   ;
  for (var key in a) {
    switch (typeof a[key]) {
      // {  /path : { ... }}
      case  object :
        app.map(a[key], route + key);
        break;
      // get: function(){ ... }
      case  function :
        if (verbose) console.log( %s %s , key, route);
        app[key](route, a[key]);
        break;
    }
  }
};

var users = {
  list: function(req, res){
    res.send( user list );
  },

  get: function(req, res){
    res.send( user   + req.params.uid);
  },

  del: function(req, res){
    res.send( delete users );
  }
};

var pets = {
  list: function(req, res){
    res.send( user   + req.params.uid +   s pets );
  },

  del: function(req, res){
    res.send( delete   + req.params.uid +   s pet   + req.params.pid);
  }
};

app.map({
   /users : {
    get: users.list,
    del: users.del,
     /:uid : {
      get: users.get,
       /pets : {
        get: pets.list,
         /:pid : {
          del: pets.del
        }
      }
    }
  }
});

app.listen(3000);

Seems that only index.js get loaded when you require("./routes") . I used the following code in index.js to load the rest of the routes:

var fs = require( fs )
  , path = require( path );

fs.readdirSync(__dirname).forEach(function(file){
  var route_fname = __dirname +  /  + file;
  var route_name = path.basename(route_fname,  .js );
  if(route_name !==  index  && route_name[0] !== "."){ 
    exports[route_name] = require(route_fname)[route_name];
  }
});

你们也可以将其组织成单元。 因此,这是一样的。

./
controllers
    index.js
    indexController.js
app.js

之后,控制器的管理人在索引库内出口控制器。

//indexController.js
module.exports = function(){
//do some set up

var self = {
     indexAction : function (req,res){
       //do your thing
}
return self;
};

然后是指数。

exports.indexController = require("./indexController");

最后,见附录一。

var controllers = require("./controllers");

app.get("/",controllers.indexController().indexAction);

I think this approach allows for clearer seperation and also you can configure your controllers by passing perhaps a db connection in.

No one should ever have to keep writing app.use( /someRoute , require( someFile )) until it forms a heap of code.

援引/确定路线的花费时间是毫无意义的。 即便你确实需要海关控制,但有时可能只是一些时候,而且对于你想要能够建立标准档案结构,并自动拥有一个模块。

Try Route Magic

在您的表态中,轮任会开始形成一种无用的庞大的法规。 你们只想用两条法典处理所有<代码>应用程序。

const magic = require( express-routemagic )
magic.use(app, __dirname,  [your route directory] )

For those you want to handle manually, just don t use pass the directory to Magic.





相关问题
How do you pass a POST method in a url manually?

I need to give an external payment site a return url to my site after a customer pays. It will be to my create action in a RESTful subscription controller. Ive tried giving the payment site this ...

Zend Framework: How to disable default routing?

I ve spent many hours trying to get this to work. And I m getting quite desperate. Would be great if someone out there could help me out :) Currently using Zend Framework 1.9.5, though I have been ...

IgnoreRoute in ASP.MVC

I am trying to access a .js file in the views directory. I have an MVC application with /Views/Home/MyControl.ascx I have a js file /Views/Home/MyControl.js I wish to reference the .js file and keep ...

ASP.NET Friendly URLs

In my research, I found 2 ways to do them. Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine ...

CodeIgniter Routing / Htaccess / URI issues

I recently joined a team of devs and I m trying to get an SVN checked out onto my local machine. Unfortunately, I ve run into some issues with links and routing. My local machine is using a WAMP ...

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

URL Mod-Rewrite

I currently have a working URL: http://example.com/security-services.php?service=fixed-camera-surveillance and then I have PHP say, $_REQUEST[ service ] to do some stuff... But I d like to ...

热门标签