Javascript node.js&;express:for循环和'app.get()`提供文章

Javascript node.js&;express:for循环和'app.get()`提供文章,javascript,node.js,express,Javascript,Node.js,Express,我正在开发一个节点js express应用程序,它使用app.get()为不同的web地址提供服务。所以app.get('/',{})服务于主页,app.get('/login'{})用于登录页面等 使用for循环以编程方式为页面提供服务(如本例中所示)是一种良好的实践吗 var a = ["a", "b", "c"]; a.forEach(function(leg) { app.get('/' + leg, function(req, res){ res.render(

我正在开发一个节点js express应用程序,它使用app.get()为不同的web地址提供服务。所以
app.get('/',{})
服务于主页,
app.get('/login'{})用于登录页面等

使用for循环以编程方式为页面提供服务(如本例中所示)是一种良好的实践吗

var a = ["a", "b", "c"];
a.forEach(function(leg) {
    app.get('/' + leg, function(req, res){
        res.render('index.ejs', {
            word : leg
        });
    });
});
index.ejs
只是

所以site.com/a、site.com/b和site.com/c都是网页

我想利用它在所有文章标题(来自存储文章的数据库)的列表上运行.foreach()


编辑:该网站允许用户提交文章,这些文章将成为数据库中的“文章”。我希望这个循环能够在新提交的文章发布后传递到它们。如果我想做
app.get('/'+newPageName)
对于用户提交的页面,在我已经使用
node server.js启动服务器之后,如何实现这一点?

利用中间件更好地处理请求。我想你会有几十个/数百个帖子,像你所做的那样为它们添加路线,这并不是很优雅

考虑以下代码;我正在定义
/posts/:legId
表单的路径。我们将把所有请求匹配到此路由,获取文章并呈现它

如果有少数路由需要定义,您可以使用正则表达式来定义它们

// dummy legs
var legs = {
  a: 'iMac',
  b: 'iPhone',
  c: 'iPad'
};

app.get('/posts/:leg', fetch, render, errors);

function fetch(req, res, next) {
  var legId = req.params.leg;

  // add code to fetch articles here
  var err;
  if (!legs[legId]) {
    err = new Error('no such article: ' + legId);
  }

  req.leg = legs[legId];
  next(err);
}

function render(req, res, next) {
  res.locals.word = req.leg;
  res.render('index');
}

function errors(err, req, res, next) {
  console.log(err);

  res.locals.error = err.message;
  // render an error/404 page
  res.render('error');
}

希望这有帮助,请随时问我任何问题。

不,您不应该生成这样的路由处理程序。这就是我们的路线

当您在Express route中启动带有
的路径组件(文件夹)时,Express将自动匹配任何遵循该模式的URL,并将实际值放入
req.params
。例如:

app.get('/posts/:leg', function(req, res, next) {
    // This will match any URL of the form /post/something
    // -- but NOT /post/something/else
    if (/* the value of req.params.leg is valid */) {
        res.render('index.ejs', { word: req.params.leg });
    } else {
        next(); // Since the user requested a post we don't know about, don't do
                // anything -- just pass the request off to the next handler
                // function.  If no handler responds to the request, Express
                // defaults to sending a 404.
    }
});
在现实世界中,您可能会通过执行数据库查找来确定
leg
参数是否有效,这需要进行异步调用:

app.get('/posts/:leg', function(req, res, next) {
    db.query(..., req.params.leg, function(err, result) {
        if (err) next(err); // Something went wrong with the database, so we pass
                            // the error up the chain.  By default, Express will
                            // return a 500 to the user.
        else {
            if (result) res.render('index.ejs', result);
            else next();
        }
    });
});

在这种情况下,转到/posts/iPad会将我带到该页面,/posts/testpage会抛出“no-this-article:testpage”错误?
req.params.leg
是否与“/post/”后的文本相同?是的,
:应用程序路由中的leg
用于设置
req.params
中的属性/posts/a将呈现imac页面,注意a/b/c是关键。您可以使用任何唯一标识post的键/id,以便可以从db中获取post。