Javascript 在节点js中使用Express模块不起作用
根据示例,我正在使用Node JS的“express”模块 当我尝试运行服务器并打开“localhost:8000”时,我得到一个错误:Javascript 在节点js中使用Express模块不起作用,javascript,node.js,Javascript,Node.js,根据示例,我正在使用Node JS的“express”模块 当我尝试运行服务器并打开“localhost:8000”时,我得到一个错误: Error: No default engine was specified and no extension was provided. at new View (/home/anr/node_modules/express/lib/view.js:41:42) at Function.app.render (/home/anr/node_modul
Error: No default engine was specified and no extension was provided.
at new View (/home/anr/node_modules/express/lib/view.js:41:42)
at Function.app.render (/home/anr/node_modules/express/lib/application.js:486:12)
at ServerResponse.res.render (/home/anr/node_modules/express/lib/response.js:798:7)
at app.post.res.send.status (/home/anr/Desktop/node js/mvc/ntwitter.js:16:7)
at callbacks (/home/anr/node_modules/express/lib/router/index.js:164:37)
at param (/home/anr/node_modules/express/lib/router/index.js:138:11)
at pass (/home/anr/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/home/anr/node_modules/express/lib/router/index.js:173:5)
at Object.router (/home/anr/node_modules/express/lib/router/index.js:33:10)
at next (/home/anr/node_modules/express/node_modules/connect/lib/proto.js:193:15)
我在那里添加了文件夹结构,现在partials文件夹包括:
chirps.html index.html styles.html view0.html
view0由基本html模板组成:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- There is a call to partial here-->
<%- partial('partials/stylesheet', stylesheets) %>
<title><%= title %></title>
</head>
<body>
<h1><%= header %></h1>
<%- body %>
</body>
</html>
因此,在所有这一切结束时,这条路线的响应代码是:
app.get('/',function(req,res){
//a conveniance method that does request headers,sends the message and ends the response
//res.send('Welcome to Node Twitter');
var title='Chirpie',
header='Welcome To Chirpie';
res.render('index',
{
locals:{
'title':title,
'header':header,
'tweets':tweets,
}
});
});
您有EJS视图,但您从未告诉Express,因此对于如何渲染它们一无所知 在app=express.createServer()之后添加以下内容:
app.set('view engine', 'ejs');
您有EJS视图,但您从未告诉Express,因此对于如何渲染它们一无所知 在app=express.createServer()之后添加以下内容:
app.set('view engine', 'ejs');
在
if(acceptsHtml(请求头['accept'])行中;{
if(acceptsHtml(req.headers['accept']))行中的语句结尾有一个分号,该分号不正确;{语句结尾有一个分号,该分号不正确。我安装了ejs,将index.html移到了视图中,它抛出了@user2309862:如果不指定扩展名(res.render('index.html'),它可能在查找index.ejs
而不是index.html
@user2309862),Express将假定扩展名是视图引擎的名称(res.render('index');//index.ejs
)。使用index.ejs,因为我没有html模块。现在,它说@user2309862res.partial()
和partial()
与Express 3.0一起发布。现在的期望是视图本身之间的视图引擎:。我安装了ejs,将index.html移动到视图中,它抛出这个@user2309862:如果不指定扩展名(res.render('index.html'),它可能正在查找index.ejs
而不是index.html
@user2309862)
),Express将假定扩展名是视图引擎的名称(res.render('index');//index.ejs
)。使用index.ejs,因为我没有html模块。现在,它说@user2309862res.partial()
和partial()
与Express 3.0一起发布。现在的期望是视图本身之间的视图引擎:。
1. Fixed `if (acceptsHtml(req.headers['accept']));`.Removed the semicolon there.
2.The files have to use `.ejs` unless the render specifies it as `.html` which requires the html module.Also move the `index.ejs` to `views`.
3.The reference to `chirp.ejs` was removed.Another point to note would be that the rendering engine is supposed to handle these calls now, the function `partial` no longer. exists,if you wish to use something like that use `include` instead.
app.get('/',function(req,res){
//a conveniance method that does request headers,sends the message and ends the response
//res.send('Welcome to Node Twitter');
var title='Chirpie',
header='Welcome To Chirpie';
res.render('index',
{
locals:{
'title':title,
'header':header,
'tweets':tweets,
}
});
});
app.set('view engine', 'ejs');