有办法稍后使用带有Node.js/Express的EJS添加CSS/JS。以下是一些建议:
<head>
标签内,以确保它们在页面加载时被加载。<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
</head>
<body>
<% include('partials/header.ejs') %>
<div class="content">
<!-- Your content here -->
</div>
<% include('partials/footer.ejs') %>
</body>
</html>
const express = require('express');
const app = express();
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
// app.js
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
<!-- views/layout.ejs -->
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
</head>
<body>
<% include('partials/header.ejs') %>
<div class="content">
<%- body %>
</div>
<% include('partials/footer.ejs') %>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" href="https://cdn.example.com/css/style.css">
<script src="https://cdn.example.com/js/script.js"></script>
</head>
<body>
<% include('partials/header.ejs') %>
<div class="content">
<!-- Your content here -->
</div>
<% include('partials/footer.ejs') %>
</body>
</html>
使用这些方法,您可以在带有Node.js/Express的EJS中添加CSS和JS文件。
领取专属 10元无门槛券
手把手带您无忧上云