Как предоставить доступ к папке после входа в систему? - PullRequest
0 голосов
/ 08 мая 2020

Это моя структура папок:

/+---home
 +---public

Моя node.js точка входа находится в папке publi c (т.е. public / server. js).

Мое требование состоит в том, что все пользователи должны войти в систему, прежде чем они смогут получить доступ к «домашней» папке. Форма входа хранится в файле public / index. html.

В целях тестирования я поместил index. html в "домашнюю" папку.

К сожалению, ничего не получил после входа в систему.

enter image description here

Не могли бы вы сказать мне, в чем проблема?

Это моя форма входа (т.е. public / index. html ).

<html>
    <head>
        <meta charset="UTF-8">
        <title>Chat Room</title>
    </head> 
    <body>
        <form method="post" action="/login">
            Nick name/Alias:<input type=text required name="alias"><br>
            Email Address:<input type=email required name="email"><br>
            <input type="submit" value="Login">
        </form>
    </body>
</html> 

Вот мой сервер. js код:

var bodyParse = require('body-parser')
var cookieParser = require('cookie-parser');
var express = require('express');
var session = require('express-session');
var app = express();
var userList={};

var http = require('http');
var serverPort = 81;
server = http.createServer(app);

server.listen(serverPort, function() {
  console.log('server up and running at %s port', serverPort);
});

var io = require('socket.io')(server);
app.use(bodyParse.urlencoded({extended:false}));
app.use(bodyParse.json());
app.use(cookieParser());
app.use(session({
    secret: 'my secret',
    resave: true,
    saveUninitialized: true
}));
app.use(express.static('public'));
app.post('/login', function(request, response) {

    var alias = request.body.alias;
    var email = request.body.email;

    if (userList[email]==null) {
        request.session.loggedin = true;
        request.session.alias=alias;
        request.session.email=email;
        response.redirect('/home');
    } else {
        response.send("<script>alert('Your email address has been used, please use anothe one.');location.href='/';</script>");
    }   
});
app.all('/home', function(request, response,next) {
    if (request.session.loggedin) {
        next();
    } else {
        response.send("<script>alert('Your have to login to view this page.');location.href='/';</script>");
    }
    response.end();
});

1 Ответ

0 голосов
/ 08 мая 2020

Извините, я решил проблему с помощью метода рендеринга express.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...