参考项目地址:https://github.com/Sam-Meech-Ward/my_ejs_notes
server.js
const express = require('express') const path = require('path') const app = express() const port = 3000 app.set("view engine","ejs") // app.set('views', path.join(__dirname, 'vieweeee')); app.get('/', (req, res) => { res.render("index.ejs",{ numberOfItterations:50 }) }) const notes=[{ contents:"123456789" }] app.get("/notes",(req,res)=>{ res.render("notes.ejs",{notes,}) }) app.use(express.static("public")) app.get("/goodbye",(req,res)=>{ res.send("goodbye") }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
package.json
{ "name": "express_ejs", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "dev": "npx nodemon server.js", "start": "node server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "ejs": "^3.1.9", "express": "^4.18.2" } }
index.ejs
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./style.css"> <title>index</title> </head> <body> <h1>my web page</h1> <ul> <% for(let i =0;i<numberOfItterations;i++){ %> <li><%= i %></li> <%}%> </ul> <h2><%= Date() %></h2> <img src="./200.jpg" alt=""> </body> </html>
notes.ejs
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>All notes</title> </head> <body> <h1>notes</h1> <%= notes[0].contents %> </body> </html>
标签:const,res,app,express,notes,ejs From: https://www.cnblogs.com/hechunfeng/p/17489005.html