appweb ejs_具有快速路线的EJS
appweb ejs
HI! Welcome to NODE AND EJS TEMPLATE ENGINE SERIES. Today, we will see how we can work with EJS and routes?
嗨! 欢迎使用NODE和EJS模板引擎系列 。 今天,我们将看到如何使用EJS和路由?
A route is like a sub domain with it's own function or web page/document.
路由就像具有其自身功能或网页/文档的子域。
For Example: On most websites, we see: .../home, .../about, .../admin
例如:在大多数网站上,我们看到: ... / home,... / about,... / admin
Read basic about EJS, read this article: Node and EJS Template Engine Series | Introduction to EJS
阅读有关EJS的基础知识,阅读本文: 节点和EJS模板引擎系列| EJS简介
In this article, we'll set up an EJS file that will render an html file with a portion of JavaScript code which is variable gotten from the express server code.
在本文中,我们将设置一个EJS文件,该文件将呈现一部分JavaScript代码的html文件,该JavaScript代码是从express服务器代码获取的 。
In our app.js file, we will setup 2 routes. The first will be our root route (or home route) and the second will be a /user/:x route.
在我们的app.js文件中,我们将设置2条路由。 第一个是我们的根路由(或本地路由),第二个是/ user /:x路由。
The x represents user request under the route user.
x表示路由用户下的用户请求。
We are going to write some code that will copy the user request and save it in a variable. To that, we will use req.params.x; where "x" represents the user request and will be saved in a variable called data.
我们将编写一些代码来复制用户请求并将其保存在变量中。 为此,我们将使用req.params.x;。 其中“ x”代表用户请求,将保存在名为data的变量中。
Finally, we then render the ejs file which will capture the content of the data variable and pass it to x in the EJS template.
最后,然后渲染ejs文件,该文件将捕获数据变量的内容并将其传递给EJS模板中的x。
More will be understood as you code.
您编写代码时会了解更多。
Open your text editor and type the following code, Save as app.js.
打开文本编辑器,然后输入以下代码, 另存为app.js。
var express = require('express');
var ejs = require('ejs');
var app = express();
app.set('view engine', 'ejs');
app.get("/", function(req, res) { // root route or home route
res.send('welcome to home page');
});
app.get("/user/:x", function(req, res) { // user route
var data = req.params.x;
res.render("user.ejs", {x: data
});
});
app.listen(3000, function() {console.log("server is listening!!!");
});
Now, let's create our ejs file.
现在,让我们创建我们的ejs文件。
Open a text editor and type the following code, Save as user.ejs
打开文本编辑器,然后输入以下代码, 另存为user.ejs
<html>
<h1><%= message%><h1>
</html>
Create a folder in your app.js directory called views as usual.
和往常一样,在app.js目录中创建一个名为views的文件夹。
Cut and paste the ejs file in the views folder.
将ejs文件剪切并粘贴到views文件夹中 。
Take Note: The folder name views is not a random word I selected but it's the reserved folder name where express checks for template engine by default.
请注意:文件夹名称视图不是我选择的随机词,而是默认情况下Express检查模板引擎的保留文件夹名称。
In our express server we used app.set () and passed in our template engine unlike our other examples.
在我们的快递服务器中,我们使用app.set()并将其传递到模板引擎中,这与其他示例不同。
Finally, initiate the app.js file with node app.js in a terminal and view the port in a browser. localhost:3000
最后,在终端中使用节点app.js初始化app.js文件,并在浏览器中查看端口。 本地主机:3000
Or for those using nodemon, use
或对于那些使用nodemon的人,使用
In our browser, we realize that whatever we type after that /user route is used by the ejs template.
在我们的浏览器中,我们意识到ejs模板使用在/ user路由之后键入的任何内容。
We can also add, some basic JavaScript functionalities such as the toUpperCase() method.
我们还可以添加一些基本JavaScript功能,例如toUpperCase()方法。
Thanks for coding with me! Feel free to drop a comment or question.
感谢您与我编码! 随意发表评论或问题。
翻译自: https://www.includehelp.com/node-js/ejs-with-express-routes.aspx
appweb ejs
appweb ejs_具有快速路线的EJS相关推荐
- Web 开发方案选择和实现
目录 一.Web 开发路线选择(HTML负责构建页面元素:JavaScript负责行为:CSS负责布局:web服务采用组件技术扩展处理业务层所有的数据计算,数据分析,数据库联结处理:文件/数据库.). ...
- 嵌入式设备web服务器比较
现在在嵌入式设备中所使用的web服务器主要有:boa.thttpd.mini_httpd.shttpd.lighttpd.goaheand.appweb和apache等. Boa 1.介绍 Boa诞生 ...
- 嵌入式web服务器预研报告(转)
现在在嵌入式设备中所使用的web服务器主要有:boa.thttpd.mini_httpd.shttpd.lighttpd.goaheand.appweb和apache等. Boa 1.介绍 Boa诞生 ...
- 荒岛余生为什么没有打开包裹_您会带到荒岛什么办公桌设置?
荒岛余生为什么没有打开包裹 Throughout life, you experience a lot of desks and a lot of desk setups. Real or virtu ...
- sql azure 语法_SQL Server + Azure:混合方案简介
sql azure 语法 We hear a lot about hybrid SQL Server environments, but how to integrate my datacenter ...
- Mapbox词汇表中文文档(查找Mapbox相关的术语及其定义)
前言 Mapbox词汇表英文文档 本文词汇表按照字母顺序排序 A access token (访问令牌) 要使用 Mapbox 的任何工具,API 或 SDK ,您需要一个 Mapbox access ...
- PredRNN++: Towards A Resolution of the Deep-in-Time Dilemma in Spatiotemporal Predictive Learning
摘要 我们提出了PredRNN ++,这是一种用于时空预测学习的递归网络.为了获得强大的短期视频动态建模能力,我们利用名为Causal LSTM的新型递归结构和级联双存储器,使我们的网络更加及时.为了 ...
- PredRNN++: Towards A Resolution of the Deep-in-Time Dilemma in Spatiotemporal Predictive Learning 翻译
PredRNN++: Towards A Resolution of the Deep-in-Time Dilemma in Spatiotemporal Predictive Learning 目录 ...
- 服务器是嵌入式系统吗,常见的几种嵌入式web服务器
嵌入式WEB服务器常见的有:Lighttpd, Shttpd, mongoose, Thttpd, Boa, Mini_httpd, Appweb, Goahead Lighttpd LibHttpd ...
最新文章
- win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面...
- STC89C52RC内部EEPROM编程
- golang 热插拨 插件_从零开始实现一个插件化框架(一)
- 利用CAGradientLayer自定义颜色渐变view
- halconC++类:HDevWindowStack
- MySQL中OR和AND的区别是什么____MySQL中or与in
- Mysql之查询优化器 EXPLAIN 详解
- python中color语句_python中的colorlog库详解
- java基于ssm的个人博客系统_一个基于 Spring Boot 的开源免费博客系统
- 微信类APP如何架构才能省流量
- Spring Boot入门篇-默认安全配置
- ActiveMQ--CVE-2015-5254
- EXCEL,如何根据数据选择合适的图表?
- 纵向时间线html,51个css时间轴
- C++如何实现二进制数据存储为灰度图
- APP开发的上线流程
- Node 笔记肆章 Express框架
- 使用python讲二进制转换为音频文件_Python使用ffmpy将amr格式的音频转化为mp3格式的例子...
- django请求生命周期,FBV和CBV,ORM拾遗,Git
- win10 安装 GPU+Tensorflow
热门文章
- unity 使用mysql实现登录注册_用mysql实现登录注册功能
- Linux配置本地yum源(RHEL8)
- 安装 Alibaba Cloud Toolkit
- 2018.10.29-2018.11.4
- 创建laravel项目
- 利用Asp.net MVC处理文件的上传下载
- 微软的SQLHelper类(含完整中文注释)
- PHP中静态方法(static)与非静态方法的使用及区别
- ActiveX控件的另类免费签名法
- 计算机技术基础 VB 答案,《计算机技术基础(VB)》武汉理工大学20春作业一