简体   繁体   English

创建nodejs在运行时表达4路由

[英]Create nodejs express 4 route on runtime

I am trying to create a route on runtime. 我试图在运行时创建一个路由。 I am trying to create a flat url structure so if we add an article it will create a slug for it and will create a new route. 我正在尝试创建一个平面网址结构,所以如果我们添加一篇文章,它将为它创建一个slug并将创建一个新的路由。

I use express 4 new router function 我用快递4新的路由器功能

app.js app.js

var express = require('express');
var routes  = require('./routes.js');
app.use('/', routes);

var server = http.createServer(app).listen(app.get('port')); 

routes.js routes.js

var express             = require('express');
var router              = express.Router();

router.get('/new'    ,site.new);
module.exports = router;

I tried creating a function in the router and calling it from the app.js also creating a function in the router while sharing the app instance accross the files 我尝试在路由器中创建一个函数并从app.js调用它,同时在路由器中创建一个函数,同时共享文件中的应用程序实例

module.exports = app; 

and calling it 并称之为

var app = require("./app.js");

It doesnt seem to work any other idea ? 它似乎没有任何其他想法吗?

update: 更新:

I have a file called helpers.js and i added the following function 我有一个名为helpers.js的文件,我添加了以下功能

  module.exports={

    addRoute:function(){   
    var express             = require('express');
    var router              = express.Router();     
    var app  = require('../app.js');
    var routes  = require('../routes.js');

    router.get('/book', function (req, res) {
      res.send('Hello World!');
    });


    app.use('/book', router);
},

I end up doing that 我最终这样做了

addRoute:function(){ addRoute:函数(){

    var express             = require('express');
    var router              = express.Router();
    var routes              = require('../routes.js');
    var app                 = require('../start-freedom1.js');

    router.get('/book'  ,function (req, res, next) {
        res.send({"data":"kaki","values":"","errors":""});
    });


    for(var layer in app._router.stack){

        if(app._router.stack[layer].name=="router"){

            app._router.stack[layer].handle.stack.splice(0, 0, router.stack[0]);
            console.log(app._router.stack[layer].handle.stack)
            break;
        }
    }

   // / app.use('/', routes);
},

the problem that i had router.get("*"..... at the end of the rout file so i always saw 404 我有router.get(“*”.....在溃败文件的末尾所以我总是看到404的问题

I think that what you are looking for is passing parameters in the URL which you can extract and then use to do some processing. 我认为您正在寻找的是在URL中传递参数,您可以提取这些参数然后用于进行一些处理。 You can do something like below: 您可以执行以下操作:

app.get('/article/:article_id', function(req, res){ art_id = req.params.article_id; //query database using art_id }

If you want to use query parameters instead (with "...../article?id=234") then you would have to use req.query . 如果你想使用查询参数(使用“..... / article?id = 234”),那么你必须使用req.query Have a look at the following page http://expressjs.com/en/api.html#req.params . 请查看以下页面http://expressjs.com/en/api.html#req.params

Request parameters are considered best practice as they are more readable and are also SEO friendly. 请求参数被认为是最佳实践,因为它们更易读并且也是SEO友好的。 You can edit your model to store the article slug as a field but should be unique, that way they can be used to query your DB. 您可以编辑模型以将文章块作为字段存储,但应该是唯一的,这样它们就可以用于查询数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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