[Node.js]26. Level 5 : Route rendering
Instead of just writing out the quote to the response, instead render the quote.ejs template, passing in the quote name and quote body.
Then finish the quote.ejs view, by printing out the quote name and body.
var express = require('express');
var app = express.createServer();
var quotes = {
'einstein': 'Life is like riding a bicycle. To keep your balance you must keep moving',
'berners-lee': 'The Web does not just connect machines, it connects people',
'crockford': 'The good thing about reinventing the wheel is that you can get a round one',
'hofstadter': 'Which statement seems more true: (1) I have a brain. (2) I am a brain.'
};
app.get('/quotes/:name', function(request, response) {
var quote = quotes[request.params.name];
// render template here
response.render('quote.ejs', {name:request.params.name, quote: quote});
});
app.listen(8080);
quote.ejs
<h2>Quote by <%= name%></h2> <blockquote>
<%= quote%>
</blockquote>
Oops, we forgot to include a layout file.
We've started one below. Finish it out by including the body of the template inside of the <body> tag. Remember to use the <%- tag so the template contents are not escaped.
<!DOCTYPE html>
<html>
<head>
<title>Quotes</title>
</head>
<body>
<%-body%>
</body>
</html>
[Node.js]26. Level 5 : Route rendering的更多相关文章
- [Node.js]25. Level 5. Route params
Create a route that responds to a GET request '/quotes/<name>', then use the param from the UR ...
- [Node.js]24. Level 5: Express, Express routes
Create an express route that responds to GET requests at the URL /tweets that responds with the file ...
- [Node.js]31. Level 7: Redis coming for Node.js, Simple Redis Commands
Let's start practicing using the redis key-value store from our node application. First require the ...
- [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question
Clients can also answer each other questions, so let's build that feature by first listening for the ...
- [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup
Below we've already created an express server, but we want to start building a real-time Q&A mod ...
- [Node.js]23. Level 4: Semantic versioning
Update the versions on your dependencies to be a little more flexible, adding the ~ in front of your ...
- [Node.js]22. Level 4: Dependency
Add two dependencies to your package.json file, connect and underscore. You'll want to useconnect ve ...
- [Node.js]33. Level 7: Persisting Questions
Let's go back to our live-moderation app and add some persistence, first to the questions people ask ...
- [Node.js]32. Level 7: Working with Lists -- Redis
As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using ...
随机推荐
- BZOJ.2738.矩阵乘法(整体二分 二维树状数组)
题目链接 BZOJ 洛谷 整体二分.把求序列第K小的树状数组改成二维树状数组就行了. 初始答案区间有点大,离散化一下. 因为这题是一开始给点,之后询问,so可以先处理该区间值在l~mid的修改,再处理 ...
- Android之安全机制
根据android四大框架来解说安全机制 代码安全 java不同于C/C++,java是解释性语言,存在代码被反编译的隐患: 默认混淆器为proguard,最新版本为4.7: proguard还可用来 ...
- CentOS的利手:“Screen”一个可以在多个进程之间多路复用一个物理终端的窗口管理器
你是不是经常需要远程登录到Linux服务器?你是不是经常为一些长时间运行的任务头疼?还在用 nohup 吗?那 么来看看 screen 吧,它会给你一个惊喜! 你是不是经常需要 SSH 或者 tele ...
- tyvj:1038 忠诚 线段树
tyvj:1038 忠诚 Time Limit: 1 Sec Memory Limit: 131072KiBSubmit: 9619 Solved: 3287 题目连接 http://www.ty ...
- CF 277.5 A.SwapSort 水题
//STL教你做人系列 #include<stdio.h> #include<iostream> #include<math.h> #include<algo ...
- 编写简单登陆和注册功能的demo时遇到的问题
一.注册功能中添加数据不成功 给数据库添加EditText中的内容后,数据库中找不到添加后的数据,并且存在字符串为空的数据 解决方法:EditText registerAccount = (EditT ...
- jQuery动画高级用法(上)——详解animation中的.queue()函数
如果你拿着一个疑问去找专业人士寻找答案,那么你的一个疑问会变成三个,因为他会用另外两个令你更加一头雾水的名词来解释你的这个疑问. 我想这是大多数,包括我在内,IT人在学习过程中碰到的最大问题.当你有一 ...
- C#线程安全的那些事
还是上一次,面试的时候提到了C#线程安全的问题,当时回答的记不太清了,大概就是多线程同是调用某一个函数时可能会照成数据发生混乱,运行到最后发现产生的结果或数据并不是自己想要的,或是跨线程调用属性或方法 ...
- UVa11187
莫勒定理,证明如下: 请结合下图看代码: #include <iostream> #include <math.h> #include <iomanip> usin ...
- JSONObject以及json(转)
一.JAR包简介 要使程序 可以运行 必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.commons- ...