my app.js的nodemon在重新加载时未刷新

wgeznvg7  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(332)

我认为这是一个非常初级的问题。。我一直在运行nodemon app.js教程并正确启动(最新版本为2.0.12)。我将express也设置为运行,但每次尝试更新任何ejs模板时。。无法重新加载以查看更改。。我必须在浏览器中手动重新加载。我的路线有什么问题吗?路线等显示良好,但我不能让现场重新加载工作。

const express = require('express');
const path = require('path');
const ejsMate = require('ejs-mate')
const methodOverride = require('method-override')
const mongoose = require('mongoose')
const Campground = require('./models/campground')

const app = express();

mongoose.connect('mongodb://localhost:27017/yelp-camp', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
})

const db = mongoose.connection
db.on("error", console.error.bind(console, "connection error:"))
db.once("open", ()=>{
    console.log("Database connected")
})

// App Logic
app.engine('ejs', ejsMate)
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))

// Forms POST object
app.use(express.urlencoded({extended: true}))
app.use(methodOverride('_method'))

app.get('/', (req, res)=>{
    res.render('home')
})

//Load all Campgrounds into ejs file
app.get('/campgrounds', async (req, res) => {
const campgrounds = await Campground.find()
    res.render('campgrounds/index', { campgrounds })
})

// Create & Read

app.get('/campgrounds/new', (req, res)=>{
    res.render('campgrounds/new')
})

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>Boiler Plate</title>
</head>
<body>

    <nav class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
          <a class="navbar-brand" href="#">YelpCamp</a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
            <div class="navbar-nav">
              <a class="nav-link active" aria-current="page" href="#">Home</a>
              <a class="nav-link" href="/campgrounds">Campgrounds</a>
              <a class="nav-link" href="/campgrounds/new">New Campground</a>
              <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
            </div>
          </div>
        </div>
      </nav>

    <main class="container">
        <%- body %>
    </main>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

</body>
</html>
w6lpcovy

w6lpcovy1#

您必须在package.json文件中添加一个脚本。

"start": "nodemon server.js -e ejs,js,css,html,jpg,png,scss"

您可以给出任何名称,而不是start,然后编写nodemon,然后使用主服务器文件名,-e告诉nodemon nodemon需要注意哪些文件类型的更改。假设您希望nodemon监视html文件类型,并且每当您更改服务器目录的html文件时,nodemon都应该重新启动服务器。

相关问题