Node.js 有人能解释一下这段代码为什么起作用吗?
因此,我在express中使用marked(一个markdown模块),我没有设置title变量或呈现任何内容,但是title变量正在发生变化-我猜-下面是我index.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>
/*
* 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 seesnext
被传递了一个错误字符串,express render的默认错误页面将其作为错误消息