javascript
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 routeres.send('welcome to home page'); }); app.get("/user/:x", function(req, res) { // user routevar 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby循环_Ruby循环
- 下一篇: java 大小写转换函数_不使用Java