2016-07-30 63 views
0

我正在學習node.js,以便可以處理我的個人項目。我也試圖開始「真實」。事實上,我嘗試起來的第一個重要功能是登錄功能。出於某種原因,在URL中彈出?時無法正常工作。使用res.sendFile()處理url中的查詢字符串

我在appServer.js代碼是一樣的東西:

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var session = require('client-sessions'); 

var users = require('./helpers/users.js'), 
    helpers = require('./helpers/helpers.js'); 

var fs = require('fs'), 
    path = require('path'); 

var https = require('https'); 

function handleIncomingRequest(req, res) 
{ 
    console.log("INCOMING REQUEST: " + req.method + " " + req.url); 

    console.log("req.url == " + req.url); 
    var questionMarkIndex = req.url.indexOf('?'); 
    console.log("questionMarkIndex == " + questionMarkIndex); 
    var filename = req.url; 
    if (questionMarkIndex != -1) filename = req.url.substr(0, questionMarkIndex); 
    serveStaticFile(filename, 
     res); 
    //serveStaticFile(req.url, res); 
} 

app//.use(express.logger('dev')) 
    .use(bodyParser.urlencoded({ extended: true })) 
    .use(session({ 
    cookieName: 'session', 
    secret: 'ckwtngqjqerrafourhpvi', 
    duration: 30 * 60 * 1000, 
    activeDuration: 15 * 60 * 1000, 
    httpOnly: true, 
    secure: true, 
    ephemeral: true 
})) 
    .get("/YALApp/*", 
     requirePageLogin, 
     function(req, res) 
     { 
      handleIncomingRequest(req, res); 
     } 
    ) 
    .get("/YALApp", 
     function (req, res) { 
      res.redirect('/YALApp/'); 
     } 
    ) 
    .post('/service/login', users.login) 
    .get("*", 
     function(req, res) 
     {   
      res.writeHead(404, { "Content-Type" : "application/json" }); 
      res.end(JSON.stringify(helpers.makeError("file_not_found", 
        "The requested file cannot be accessed: " + req.url), null, '\t') + '\n'); 
     } 
    ); 
app.listen(8080); 
/*https.createServer(options, app).listen(8080, function() 
{ 
    console.log("server listening on port 8080"); 
}); 
*/ 
function serveStaticFile(file, res) 
{ 
    console.log("file == " + file); 
    var url = '/var/www/html' + file; 
    if (url.charAt(url.length - 1) == '/' || url.lastIndexOf('.') == -1) 
    { 
     console.log(res.sendFile); 
     res.sendFile(url + '/index.html'); 
     return; 
    } 
    res.sendFile(url); 
} 

初始靜態頁面(網址:/YALApp/loginPage.html)負載就好了,但它的依賴不會(並因此頁面不能正常運行) 這裏是靜態頁面:Trying the "smart" sendFile method

這裏發生的事情:

<!DOCTYPE html> 
<html> 
    <head> 
     <!-- Linking to define the colors (up one level, and in theme) --> 
     <script src="../jQueryLib/theme/external/jquery/jquery.js"></script> 
     <link href="../jQueryLib/theme/jquery-ui.css" rel="stylesheet" > 
     <!-- Linking to setup the page --> 
     <link href="css/loginPage.css" rel="stylesheet"> 
     <!-- Providing UI functionality --> 
     <script type="text/javascript" src="../jQueryLib/theme/jquery-ui.js"></script> 
     <!-- jQuery UI touch-punch --> 
       <script type="text/javascript" src="../jQueryLib/theme/jquery.ui.touch-punch.min.js"></script> 
     <!-- page functionality --> 
     <script src="js/loginPage.js"></script> 
     <title>Group Management Login Page</title> 
    </head> 
    <body> 
     <h1 class="center">Group Management Tool</h1> 
     <div id="login" class="pageCenter"> 
      <form id="loginForm"> 
       <h2 class="ui-widget-header row">Login</h2> 
       <span class="row"> 
        <label for="loginUserName" class="third">User name</label> 
        <input type="text" id="loginUserName" class="twoThirds"></input> 
       </span> 
       <span class="rowTight"> 
        <label for="loginPassword" class="third">Password</label> 
        <input type="password" id="loginPassword" class="twoThirds"></input> 
       </span> 
       <span class="rowTight"> 
        <!-- TODO: implement "forgot username" feature --> 
        <a id="forgotUsernameLink" class="center half" href="">Forgot username?</a> 
        <!-- TODO: implement "forgot password" feature --> 
        <a id="forgotPasswordLink" class="center half" href="">Forgot password?</a> 
       </span> 
       <span class="row"> 
        <input type="submit" id="loginBtn" class="third" value="Login" /> 
        <!--<button id="protoLogin" title="Populates login form with login data for the prototype" value="Prototype"--> 
       </span> 
      </form> 
     </div> 
    </body> 
</html> 

的努力結果?!?!?

+1

我建議停止解析URL自己。 NodeJS帶有一個URL包,允許進行各種URL解析和操作。例如:'url.parse(req.url,true)'。想了解更多信息,請訪問https://nodejs.org/api/url.html – Dogoku

+0

@Dogoku 不錯的想法,但它不適合我:( –

+0

)您是否需要該軟件包?var url = require('url'); ''''''''NodeJS暴露了很少的全局變量,你必須要求大部分東西 – Dogoku

回答

0

我想我發現了錯誤:我試圖通過處理每個其他請求的GET請求來避免安全災難。也就是說,當我「退出」YALApp文件夾時,我至少可以訪問phpMyAdmin文件夾,並且只需從中請求文件即可下載該文件。我讓它提供了404錯誤。

這很容易解決,因爲我所要做的只是爲/jQueryLib/*請求添加路由,並重復我爲/YALApp/*所做的操作。