Express-Edge 模板引擎查找错误 URL
Express-Edge Template Engine looks for wrong URL
我认为我的 app.set('views', ${__dirname}/views
) 配置有问题,因为当我最近请求 URL 和 firstRoute/someRoute
时它会查找 /firstRoute/css
。除此之外,一切都只适用于单一路线,如 /
或 /someRoute
等
'http://localhost:4000/posts/vendor/bootstrap/css/bootstrap.min.css'
而不是:
'http://localhost:4000/vendor/bootstrap/css/bootstrap.min.css'
这是我的 index.js 文件:
const path = require('path');
const expressEdge = require("express-edge");
const express = require("express");
const edge = require("edge.js");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const fileUpload = require("express-fileupload");
const expressSession = require('express-session');
const connectMongo = require('connect-mongo');
const connectFlash = require("connect-flash");
const createPostController = require('./controllers/createPost');
const homePageController = require('./controllers/homePage');
const storePostController = require('./controllers/storePost');
const getPostController = require('./controllers/getPost');
const createUserController = require("./controllers/createUser");
const storeUserController = require('./controllers/storeUser');
const logoutController = require("./controllers/logout");
const loginController = require("./controllers/login");
const loginUserController = require('./controllers/loginUser');
const app = new express();
app.use(express.static("public"));
app.use(expressEdge);
app.set('views', `${__dirname}/views`);
mongoose.connect('mongodb://localhost:27017/aomerk', {
useCreateIndex : true,
useNewUrlParser: true })
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err));
app.use(connectFlash());
const mongoStore = connectMongo(expressSession);
app.use(expressSession({
secret: 'secret',
saveUninitialized : true,
store: new mongoStore({
mongooseConnection: mongoose.connection
})
}));
app.use('*', (req, res, next) => {
edge.global('auth', req.session.userId)
next()
});
app.use(fileUpload());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const storePost = require('./middleware/storePost');
const auth = require("./middleware/auth");
const redirectIfAuthenticated = require('./middleware/redirectIfAuthenticated')
app.get("/", homePageController);
app.get("/post/:id", getPostController);
app.get("/posts/new", auth, createPostController);
app.post("/posts/store", auth, storePost, storePostController);
app.get("/auth/login", redirectIfAuthenticated, loginController);
app.post("/users/login", redirectIfAuthenticated, loginUserController);
app.get("/auth/register", redirectIfAuthenticated, createUserController);
app.post("/users/register", redirectIfAuthenticated, storeUserController);
app.get("/auth/logout", redirectIfAuthenticated, logoutController);
app.listen(4000, () => {
console.log("App listening on port 4000");
});
问题出在我的主模板文件的头部。我正在加载 css 个文件:
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
但我应该
<link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
开头的 '/' 告诉 href 它应该寻找根。
我认为我的 app.set('views', ${__dirname}/views
) 配置有问题,因为当我最近请求 URL 和 firstRoute/someRoute
时它会查找 /firstRoute/css
。除此之外,一切都只适用于单一路线,如 /
或 /someRoute
等
'http://localhost:4000/posts/vendor/bootstrap/css/bootstrap.min.css'
而不是:
'http://localhost:4000/vendor/bootstrap/css/bootstrap.min.css'
这是我的 index.js 文件:
const path = require('path');
const expressEdge = require("express-edge");
const express = require("express");
const edge = require("edge.js");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const fileUpload = require("express-fileupload");
const expressSession = require('express-session');
const connectMongo = require('connect-mongo');
const connectFlash = require("connect-flash");
const createPostController = require('./controllers/createPost');
const homePageController = require('./controllers/homePage');
const storePostController = require('./controllers/storePost');
const getPostController = require('./controllers/getPost');
const createUserController = require("./controllers/createUser");
const storeUserController = require('./controllers/storeUser');
const logoutController = require("./controllers/logout");
const loginController = require("./controllers/login");
const loginUserController = require('./controllers/loginUser');
const app = new express();
app.use(express.static("public"));
app.use(expressEdge);
app.set('views', `${__dirname}/views`);
mongoose.connect('mongodb://localhost:27017/aomerk', {
useCreateIndex : true,
useNewUrlParser: true })
.then(() => 'You are now connected to Mongo!')
.catch(err => console.error('Something went wrong', err));
app.use(connectFlash());
const mongoStore = connectMongo(expressSession);
app.use(expressSession({
secret: 'secret',
saveUninitialized : true,
store: new mongoStore({
mongooseConnection: mongoose.connection
})
}));
app.use('*', (req, res, next) => {
edge.global('auth', req.session.userId)
next()
});
app.use(fileUpload());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const storePost = require('./middleware/storePost');
const auth = require("./middleware/auth");
const redirectIfAuthenticated = require('./middleware/redirectIfAuthenticated')
app.get("/", homePageController);
app.get("/post/:id", getPostController);
app.get("/posts/new", auth, createPostController);
app.post("/posts/store", auth, storePost, storePostController);
app.get("/auth/login", redirectIfAuthenticated, loginController);
app.post("/users/login", redirectIfAuthenticated, loginUserController);
app.get("/auth/register", redirectIfAuthenticated, createUserController);
app.post("/users/register", redirectIfAuthenticated, storeUserController);
app.get("/auth/logout", redirectIfAuthenticated, logoutController);
app.listen(4000, () => {
console.log("App listening on port 4000");
});
问题出在我的主模板文件的头部。我正在加载 css 个文件:
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
但我应该
<link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
开头的 '/' 告诉 href 它应该寻找根。