簡體   English   中英

express.js中的URL解析

[英]url parsing in express.js

我正在嘗試在節點服務器上學習express.js,並且嘗試將同一內容從http模塊復制到express框架中。

進一步說明:在開始使用Express之前,我使用的是第一個代碼,在該代碼中,我在帶有以下網頁的“站點”文件夾中有一個文件樹,例如

e:
|_node.js
   |_node_modules
   |_site
   | |_abc
   | | |_123.html
   | | |_456.html
   | | |_789.html
   | |_cde
   | | |_123.html
   | | |_456.html
   | | |_789.html
   | |_abc.html
   | |_cde.html
   |_server.js

而且我通常只通過讓localhost:8080 / abc / 123訪問/site/abc/123.html來訪問它們。

var http = require('http');
var url = require('url');
var fs = require('fs');
var port = 8080;

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "./site" + q.pathname + ".html";
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    }  
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(port);
console.log('Running on port ' + port);

在這里,我嘗試這樣做

var url = require('url');
var fs = require('fs');
var express = require('express');
var app = express();
var port = 8080;
var site = "/site"

app.get('*', function (req, res) {
  var q = url.parse(req.url, true);
  var filename = site + q.pathname + ".html";
    fs.readFile(filename, function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    }  
    res.writeHead(200, {'Content-Type': 'text/html'});
      res.sendFile(filename, { root: __dirname + site } );
    return res.end();
  });
})

var server = app.listen(port, function () {
    console.log('Running on port ' + port);
})

但是現在有了express框架,當我鍵入localhost:8080 / abc / 123時,但在輸入http:// localhost:8080 / abc時僅返回404 Not Found

顯然錯誤包含

{[Error: ENOENT: no such file or directory, open 'e:\site\abc.html']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'e:\\site\\abc.html'}

完整的路徑應該是

E:\\ node.js的\\網站\\ abc.html

並且server.js位於e:\\ node.js

為什么它跳回到e:而不是e:\\ node.js

現在,我猜是“ root:__dirname”引起了錯誤,我應該用什么替換它,以便它從e:/node.js而不是e:開始?

更改:

var site = "/site"

至:

var site = "site"

root選項僅適用於相對文件名,但是以'/'開頭的路徑是文件系統根目錄的絕對路徑。

除非您需要對文件做更多的事情,否則,如果只提供文件,則最好使用express.static()來提供文件,該文件可以配置為不公開.html擴展名,如下所示:

https://stackoverflow.com/a/37990843/2106611

這可能不是唯一的問題,但有一件事是

app.get('/', ... 

將僅匹配根URL,因此它將匹配localhost:8080/而不匹配localhost:8080/foo或任何其他路徑。

app.get 接受字符串,路徑模式或正則表達式

要匹配您需要做的任何事情:

app.get('/*', ... 

或者,如果您只想匹配以某個細分開頭的路徑,則可以執行以下操作:

app.get('/some-path/*', ...

並且將匹配localhost:8080/some-path/localhost:8080/some-path/foolocalhost:8080/some-path/bar等。

編輯:

關於為什么得到錯誤的文件路徑,如果仔細看,您會發現嘗試發送文件時只使用__dirname ,而在閱讀文件時卻沒有使用!

所以你有了:

var filename = site + q.pathname + ".html";
    // This is not reading from the correct path -
    // it's missing __dirname!
    fs.readFile(filename, function(err, data) {

可能應該更改為

var filename = __dirname + site + q.pathname + ".html";
    // This is now using __dirname which should be the
    // the directory of your server module
    fs.readFile(filename, function(err, data) {

然后,由於您已經將data在變量中,因此無需使用sendFile ,則可以使用與示例類似的代碼:

res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM