在express.js中进行网址解析

时间:2019-04-29 15:57:22

标签: javascript node.js express

我正在尝试在节点服务器上学习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);
})

但是现在当我输入localhost:8080 / abc / 123时使用express框架,但是它只返回404 Not Found 输入http://localhost:8080/abc

显然错误包含

{[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 \ site \ abc.html

和server.js位于 e:\ node.js

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

现在我猜测是“ root:__dirname”引起了错误,我应该用什么替换它,以便它从e:/node.js而不是e:开始?

2 个答案:

答案 0 :(得分:1)

更改:

var site = "/site"

收件人:

var site = "site"

root选项仅适用于相对文件名,但是以'/'开头的路径是文件系统根目录的绝对路径。

除非只需要对文件进行其他处理,否则,如果只为它们提供服务,则最好使用express.static()来为其提供服务,该文件可以配置为不公开.html扩展名,如此处所示:

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

答案 1 :(得分:0)

这可能不是唯一的问题,但唯一的问题是

app.get('/', ... 

仅匹配根URL,因此它将匹配localhost:8080/,而不匹配localhost:8080/foo或任何其他路径。

app.get accepts a string, path pattern or regular expression

要匹配任何内容,您需要执行以下操作:

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) {

然后,由于变量中已经包含sendFile,因此不需要使用data,则可以使用与示例类似的代码:

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