简体   繁体   中英

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.

I use express 4 new router function

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

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

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

  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(){

    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

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. 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 . Have a look at the following page http://expressjs.com/en/api.html#req.params .

Request parameters are considered best practice as they are more readable and are also SEO friendly. 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.

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