我正在使用express-handlebars 来渲染一些html 页面。浏览器找不到我链接到的 css 文件。这是我的服务器文件的样子:
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const _ = require('lodash');
const bodyParser = require('body-parser');
const socketIO = require('socket.io');
const http = require('http');
var static = require('node-static');
var {mongoose} = require('./db/mongoose.js');
var {User} = require('./models/user');
const publicPath = path.join(__dirname, '../public');
var app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
var server = http.createServer(app);
var io = socketIO(server);
const port = process.env.PORT || 3000;
app.use('/', express.static(publicPath));
app.use(bodyParser.json());
app.get('/', (req, res) …Run Code Online (Sandbox Code Playgroud) 我编写了一个辅助函数来帮助我格式化 URL,它是一些对象属性的组合。如何在车把视图中连接此属性?
辅助功能
const url = (link)=>{
return process.env.URL+'/'+link.replace(/ /gi,'-').toLowerCase();
};
Run Code Online (Sandbox Code Playgroud)
我的看法
<a href="{{url 'samples/'+this.name+'/'+this.class+'/'+this.id}}">{{this.name}}</a>
Run Code Online (Sandbox Code Playgroud) node.js express handlebars.js handlebarshelper express-handlebars
尝试使用nodemailer发送模板电子邮件并使用把手制作模板。
电子邮件确实已发送,但电子邮件的样式不正确。看起来像这样
我已附上我在车把模板中编写的内容
<!DOCTYPE html>
<html>
<head>
<title>Welcome to</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Cabin');
body { margin: 0; padding: 0; left: 0; top: 0; background-color: white; }
h1 { font-family: 'Cabin'; font-weight: 600; color: rgba(5,45,84,1); margin-left: 2px;}
p { font-family: 'Cabin'; font-weight: 400; font-size: 11pt; color: rgba(5,45,84,1); margin-left: 2px; }
h3 {
margin-top: 50px;
color: rgba(5,45,84,.25);
font-family: 'Cabin';
font-weight: 400;
font-size: 10pt;
width: 80vw;
}
h2 {
text-align: center;
margin-top: 25px;
margin-bottom: 10px;
color: rgba(5,45,84,1);
font-family: 'Cabin';
font-weight: 400;
font-size: 10pt;
} …Run Code Online (Sandbox Code Playgroud) 我正在关注有关如何呈现 HTML 页面的NodeJS 教程,但我似乎无法使其工作。我安装了所有要求并使用了该--save选项,因此它位于我的node_modules文件夹中。在我npm start访问我的后localhost,这是我收到的错误消息:
Error: No default engine was specified and no extension was provided.
at new View (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\view.js:62:11)
at EventEmitter.render (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\response.js:966:7)
at app.get (C:\Users\Billy\NodeJSProjects\HelloWorld\app\index.js:25:12)
at Layer.handle [as handle_request] (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Billy\NodeJSProjects\HelloWorld\node_modules\express\lib\router\index.js:335:12)
Run Code Online (Sandbox Code Playgroud)
./index.js
require('./app/index')
const path = require('path')
const express = require('express')
const exphbs = require('express-handlebars')
const app = express()
app.engine('.hbs', exphbs({ …Run Code Online (Sandbox Code Playgroud) 我express-handlebars在我的项目中使用并有以下问题:
问题
我希望能够<script>从视图内部调用的局部视图向我的整体视图头部添加其他此类标签。
例子:
风景
{{#layout/master}}
{{#*inline "head-block"}}
<script src="some/source/of/script">
{{/inline}}
...
{{>myPartial}}
{{/layout/master}}
Run Code Online (Sandbox Code Playgroud)
该视图正在扩展我用作布局的另一个部分(布局/主)。它通过内联部分符号将其内容添加到那个 head 块中,这很好用
部分“我的部分
<script src="another/script/src/bla"></script>
<h1> HELLO </h1>
Run Code Online (Sandbox Code Playgroud)
现在,我希望将其中的特定脚本标记添加到我的视图头块中。我尝试通过@root符号进行,但只能在那里引用上下文。不改变任何东西。
我知道我可以使用 jquery 或类似的方法来通过引用文档头等来添加内容。但我想知道这是否可以通过 Handlebars 实现。
我确实怀疑它以任何方式存在。但是,如果您有任何想法或建议,请务必按照我的方式发送!非常感谢!!!
我使用 Express Handlebars 作为节点的模板引擎。我知道可以选择添加 HTML 注释,但是有没有办法添加不会打印在最终源代码上的开发人员注释?
这是我发现的:
{{! This comment will not be in the output }}
<!-- This comment will be in the output -->
Run Code Online (Sandbox Code Playgroud)
但寻找:
{{! This comment should only be visible in the source file, not in the client side }}
Run Code Online (Sandbox Code Playgroud)
类似于 PHP 中视图中可以完成的操作:
<?php
/* Comment here */
?>
Run Code Online (Sandbox Code Playgroud) 问题其实不难理解,就是不知道Express中如何实现handlebars。
这就是我已经编码的内容:
var express = require('express');
var app = express();
app.get('/', function (req, res, next) {
return res.render('index');
});
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,如何将车把设置为 Express 的应用程序引擎?