我在登录页面上使用背景图像,但在部署到heroku时没有加载。它在Localhost上运行良好。当我打开调试器窗口并在浏览器中打开图像url时,它会重定向到登录屏幕。如何避免在登录屏幕上对背景图像进行身份验证检查。
登录模板代码如下:
div(class="container" style="background-image:url(../images/school-wallpaper.jpg);height:650px;width:100%")
form(class="form-signin" method="post")
h2(class="form-signin-heading") Please sign in
label(for="inputEmail", class="sr-only")
Email address
input(type="email", id="inputEmail", name="email" class="form-control", placeholder="Email address", required, autofocus)
label(for="inputPassword", class="sr-only") Password
input(type="password", id="inputPassword", name= "password", class="form-control", placeholder="Password", required)
br
button(class="btn btn-lg btn-primary btn-block", type="submit") Sign in
#error
if error
label.error #{error}
而服务器端的实现是:
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "http://localhost");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if(req.url.indexOf("/login")==0)
next()
else{
if(req.session.userProile){
next();
}
else{
res.redirect('/login');
}
}
});
您的错误是对Access-Control-Allow-Origin
标头进行了硬编码,这里是:
res.header("Access-Control-Allow-Origin", "http://localhost");
一旦部署到heroku,主机就不再是localhost
了。我建议您将主机名存储在环境变量中,这在生产或开发过程中会有所不同。例如,您可以使用dotenv模块使此过程更加容易。