我正在啟動專案 ts、node、express。有沒有辦法將打字稿檔案添加到在客戶端運行的 html/ejs(所以我可以訪問檔案等,就像在常規 js 腳本中一樣)?
所以它會是這樣的:
- 帶有 express 的節點服務器,它回傳 html 頁面并處理用戶登錄/注冊請求。
- 在客戶端腳本中的用戶活動句柄上添加、洗掉、更改 html 中的元素。
我將不勝感激指出我在整個節點中誤解的內容,如果我這樣做了,請表達概念。
uj5u.com熱心網友回復:
詳細解釋請參考這個鏈接: Serving html from express
您基本上可以從 express 提供 html 頁面。在根請求處理程式上,您可以服務器 index.html
app.get('/', (req, res)=>{res.render('index.html');}
現在在 index.html 頁面中有一個指向 /index 路由示例的鏈接:
<a href="/login">
現在,在服務器代碼上有一個路由處理程式
app.get('/login', (req, res)=>{res.render('login.html');}
login.html 現在可以有一個帶有動作的表單
<form action="/signin" method="post">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="login">
</form>
現在,在服務器代碼上有一個路由處理程式
app.post('signin', (req, res)=>{//handle logic here});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/422733.html
標籤:
上一篇:根據創建日期選擇不同的最新記錄
下一篇:可以在ejs中動態創建整個表嗎?
