Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 有人能解释一下这段代码为什么起作用吗?_Node.js_Express - Fatal编程技术网

Node.js 有人能解释一下这段代码为什么起作用吗?

Node.js 有人能解释一下这段代码为什么起作用吗?,node.js,express,Node.js,Express,因此,我在express中使用marked(一个markdown模块),我没有设置title变量或呈现任何内容,但是title变量正在发生变化-我猜-下面是我index.js路线的代码: /* * GET home page. */ exports.index = function(req, res, marked){ marked('Why is this even __working__?.'); }; 下面是我的index.ejs的代码 <!DOCTYPE html>

因此,我在express中使用marked(一个markdown模块),我没有设置title变量或呈现任何内容,但是title变量正在发生变化-我猜-下面是我index.js路线的代码:

/*
 * GET home page.
 */

exports.index = function(req, res, marked){
  marked('Why is this even __working__?.');
};
下面是我的index.ejs的代码

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
  </body>
</html>

欢迎来到

下面是我在浏览器中得到的信息,尽管我多次重新启动应用程序!!:


您误解了connect/express中间件功能签名。你有:

exports.index = function(req, res, marked){
但这是不对的。它是
req,res,next
,没有
标记
。因此,您没有呈现标记,而是将字符串传递给
next()
,而connect/express将其视为错误。一旦删除了命名错误的函数参数混淆,代码就会执行此操作:

exports.index = function(req, res, next){
    next('Why is this even __working__?.');
}
因此,connect sees
next
被传递了一个错误字符串,express render的默认错误页面将其作为错误消息