我认为我的app.set('views', ${__dirname}/views
)配置有问题,因为当我最近请求带有/firstRoute/css
的URL时,它会寻找firstRoute/someRoute
。除此之外,所有内容仅适用于/
或/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");
});
答案 0 :(得分:0)
问题出在我的主模板文件的头部。我正在加载CSS文件:
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
但是我应该有
<link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
开头的'/'告诉href它应寻找根。