Lag en node app med ejs



Lag en node app med ejs

av Ole Petter den 30. juni 2022

Sist oppdatert: 30 juni, 2022 kl 12:29

Contents

Kildekode

package.json

{
  "name": "node-mongoose",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "ejs": "^3.1.8",
    "express": "^4.18.1",
    "nodemon": "^2.0.18"
  },
  "devDependencies": {},
  "scripts": {
    "start": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

app.js

const express = require('express')

const app = express()

const testRoutes = require("./routes/test")

app.set('view engine', 'ejs')
app.set('views', 'views')

app.use("/test", testRoutes)

app.get("/", (req, res) => {
    res.render("index")
})

app.get("/ejs", (req, res) => {
    res.render("index")
})

app.listen(3000)

index.ejs

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Home page</h1>
</body>
</html>

test.js

const express = require("express")

const router = express.Router()

router.get("/test", (req, res) => {
    res.send("Test")
})

module.exports = router
Legg igjen en kommentar