在If中使用Express Route?

时间:2016-08-16 23:45:35

标签: node.js express

如果用户在数组中,是否可以使用Express来呈现用户页面?像

这样的东西
var express = require("express");
var app = express();
var users = ["Community", "bjskistad", "sindresorhus"]

app.get(null, function(req, res){
    for(var i in array){
        if(i in array){
            app.get("/"+i, function(req, res){
                res.send(//whatever);
            }
        }
    }
}

这可能吗?或者Express只显示静态页面?

1 个答案:

答案 0 :(得分:0)

有效的方法是

var express = require("express");
var app = express();
var users = ["Community", "bjskistad", "sindresorhus"]

app.get("/:user", function(req, res){ //the username is a path parameter
    var curentUser = req.params.user; //retrieve the username
    if(users.indexOf(currentUser)>-1){ //check if user is in array
        res.send(/*whatever*/);       //send the expected page
        //or 
        //res.redirect(/*whatever else*/); //or redirect to another url
    } else {
        res.send(/*403 page*/);
    }
}