Javascript 将node.js中的express.static(_dirname)函数与express一起使用时重定向到错误的html页面
我有一个名为login.html的登录页面和一个名为index.html的索引页面。我想进行身份验证,并且只有连接的用户才能访问索引页 我没有在登录HTML页面上实现post方法。我必须通过以下url手动发送登录用户名和密码:Javascript 将node.js中的express.static(_dirname)函数与express一起使用时重定向到错误的html页面,javascript,html,node.js,express,Javascript,Html,Node.js,Express,我有一个名为login.html的登录页面和一个名为index.html的索引页面。我想进行身份验证,并且只有连接的用户才能访问索引页 我没有在登录HTML页面上实现post方法。我必须通过以下url手动发送登录用户名和密码: http://localhost:2222/?username=a&password=b 一切正常,但我无法在index.html中看到我的css、js和其他一些文件。为了解决这个问题,我在代码的开头添加了以下内容: app.use(express.static
http://localhost:2222/?username=a&password=b
一切正常,但我无法在index.html中看到我的css、js和其他一些文件。为了解决这个问题,我在代码的开头添加了以下内容:
app.use(express.static(__dirname));
问题是,如果现在转到localhost:2222,它将显示index.html文件,而不是login.html文件。即使我使用:
app.get('/', function (req, res) {
res.redirect('/login');
});
它是怎么来的?如何解决此问题?
完整代码为:
var express = require("express");
var port = process.env.PORT || 2222;
var app = express();
app.use(express.static(__dirname));
var session = require('express-session')
app.use(session({
secret: 'keyboardcat',
resave: true,
saveUninitialized: true
}));
function checkAuth(req, res, next) {
if (!req.session.user_id) {
res.sendfile('login.html');
} else {
next();
}
}
app.get('/', function (req, res) {
res.redirect('/login');
});
app.get("/login", function(req, res) {
if (req.query.username === 'a' && req.query.password === 'b') {
req.session.user_id = req.query.username;
res.redirect('index');
} else {
res.sendfile('login.html');
}
});
app.get('/index', checkAuth, function(req, res){
res.sendfile('index.html');
});
app.get('/logout', function (req, res) {
delete req.session.user_id;
res.redirect('/login');
});
我的文件树如下:index.html、login.html和server.js位于名为server的文件夹中。此文件夹服务器中还有4个文件夹:JS、CSS、Images和Random。您必须将根目录定义为提供静态内容的第一个参数:
app.use('/', express.static(__dirname));
或者,您也可以使用:
app.use(express.static(__dirname + '/'));
您必须将根目录定义为服务静态内容的第一个参数:
app.use('/', express.static(__dirname));
或者,您也可以使用:
app.use(express.static(__dirname + '/'));
如果您使用的是
express.static
,修复目录结构非常重要,因为此时可以运行http://localhost:2222/server.js
并下载服务器文件,这是您当前存储机密的地方
我建议您创建一个server/static
目录,将所有HTML、CSS、JS、图像和其他资产放在其中,然后更改此行
app.use(express.static(__dirname));
到
此外,您决不能像当前使用GET
参数那样通过GET发送身份验证数据http://localhost:2222/?username=a&password=b
。您需要通过编辑此行将此路由更改为POST请求:
app.get("/login", function(req, res) {
到
您可能需要将HTML中的
表单
从
更改为
如果您使用express.static
,则修复目录结构非常重要,因为此时可以运行http://localhost:2222/server.js
并下载服务器文件,这是你目前储存秘密的地方
我建议您创建一个server/static
目录,将所有HTML、CSS、JS、图像和其他资产放在其中,然后更改此行
app.use(express.static(__dirname));
到
此外,您决不能像当前使用GET
参数那样通过GET发送身份验证数据http://localhost:2222/?username=a&password=b
。您需要通过编辑此行将此路由更改为POST请求:
app.get("/login", function(req, res) {
到
您可能需要将HTML中的
表单
从
更改为
您在发布应用程序时使用的是静态项目文件夹。使用(express.static(u dirname))代码>。ExpressJS使用index.html
作为默认索引页。因此,您需要将index.html
重命名为类似main.html
的其他名称,并使用res.sendfile('main.html')代码>
替代解决方案:
创建一个文件夹,比如说public
,并将所有静态内容(js、css和图像)放入public
文件夹,请不要将html文件放入public
文件夹,并使用app.use(express.static(uu dirname)+'/public')
在发布应用程序时,您正在使用项目文件夹进行静态操作。使用(express.static(_dirname))代码>。ExpressJS使用index.html
作为默认索引页。因此,您需要将index.html
重命名为类似main.html
的其他名称,并使用res.sendfile('main.html')代码>
替代解决方案:
创建一个文件夹,比如说public
,并将所有静态内容(js、css和图像)放入public
文件夹,请不要将html文件放入public
文件夹,并使用app.use(express.static(uu dirname)+'/public')
这不是一个要求,它是一个虚拟路径前缀选项,如果您想将例如/css/main.css
更改为/helloworld/css/main.css
,您可以使用app.use('/helloworld',express.static(\uu dirname))
,这不是要求,这是一个虚拟路径前缀选项,如果您想将例如/css/main.css
更改为/helloworld/css/main.css
,您可以使用app.use('/helloworld',express.static(u_dirname))
。这是实际的解决方案。我刚刚将index.html重命名为main.html,它成功了!这才是真正的解决办法。我刚刚将index.html重命名为main.html,它成功了!我做了你建议的改变。谢谢:)我做了你建议的改变。谢谢:)