Javascript Node.js:从请求获取路径

Javascript Node.js:从请求获取路径,javascript,node.js,express,Javascript,Node.js,Express,我有一个名为“localhost:3000/returnStat”的服务,它应该以文件路径作为参数。例如“/BackupFolder/toto/tata/titi/myfile.txt” 如何在浏览器上测试此服务? 例如,如何使用Express格式化此请求 exports.returnStat = function(req, res) { var fs = require('fs'); var neededstats = []; var p = __dirname + '/' + req.pa

我有一个名为“localhost:3000/returnStat”的服务,它应该以文件路径作为参数。例如“/BackupFolder/toto/tata/titi/myfile.txt”

如何在浏览器上测试此服务? 例如,如何使用Express格式化此请求

exports.returnStat = function(req, res) {

var fs = require('fs');
var neededstats = [];
var p = __dirname + '/' + req.params.filepath;

fs.stat(p, function(err, stats) {
    if (err) {
        throw err;
    }
    neededstats.push(stats.mtime);
    neededstats.push(stats.size);
    res.send(neededstats);
});
};
我还没有测试过你的代码,但其他的东西都可以

如果要从请求url获取路径信息

 var url_parts = url.parse(req.url);
 console.log(url_parts);
 console.log(url_parts.pathname);
1.如果您获取的URL参数仍然无法读取文件,请在我的示例中更正您的文件路径。如果您将index.html放在与服务器代码相同的目录中,它将工作

2.如果您有大文件夹结构,希望使用node托管,那么我建议您使用expressjs之类的框架

如果希望原始解决方案指向文件路径

var http = require("http");
var url = require("url");

function start() {
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

exports.start = start;

来源:

只需调用
req.url
。那就行了。您将得到类似于
/something?bla=foo
的内容,您可以在
app.js
文件中使用它

var apiurl = express.Router();
apiurl.use(function(req, res, next) {
    var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
    next();
});
app.use('/', apiurl);

基于@epegzz对正则表达式的建议

( url ) => {
  return url.match('^[^?]*')[0].split('/').slice(1)
}
返回包含路径的数组

req.protocol + '://' + req.get('host') + req.originalUrl


req.protocol+”://“+req.headers.host+req.originalUrl
//我喜欢这一个,因为它从代理服务器生存下来,获得原始主机名

在使用express request时结合了上述解决方案:

let url=url.parse(req.originalUrl);
let page = url.parse(uri).path?url.parse(uri).path.match('^[^?]*')[0].split('/').slice(1)[0] : '';
这将处理以下所有情况:

localhost/page
localhost:3000/page/
/page?item_id=1
localhost:3000/
localhost/
等等。一些例子:

> urls
[ 'http://localhost/page',
  'http://localhost:3000/page/',
  'http://localhost/page?item_id=1',
  'http://localhost/',
  'http://localhost:3000/',
  'http://localhost/',
  'http://localhost:3000/page#item_id=2',
  'http://localhost:3000/page?item_id=2#3',
  'http://localhost',
  'http://localhost:3000' ]
> urls.map(uri => url.parse(uri).path?url.parse(uri).path.match('^[^?]*')[0].split('/').slice(1)[0] : '' )
[ 'page', 'page', 'page', '', '', '', 'page', 'page', '', '' ]

利用WebAPI的更现代的解决方案:

(请求、回复)=>{
const{pathname}=新URL(req.URL | |“”,`https://${req.headers.host}`)
}

Yep创建REST调用参见本文,使用浏览器,仅用于快速测试。我的代码可以工作,但如何检索文件路径(req参数,如“/BackupFolder/toto/tata/titi/myfile.txt”)。注意,
url.parse()
不赞成使用
new url()
,但是,如果您传递它,则后者将失败。
'/'
=/示例中的路径是
/something
。使用
req.url.match(“^[^?]*”)[0]
仅显示路径。我感觉有一个内置的不需要凌乱的regexUse
主机名
而不是主机名。使用
主机名
而不是主机名。
> urls
[ 'http://localhost/page',
  'http://localhost:3000/page/',
  'http://localhost/page?item_id=1',
  'http://localhost/',
  'http://localhost:3000/',
  'http://localhost/',
  'http://localhost:3000/page#item_id=2',
  'http://localhost:3000/page?item_id=2#3',
  'http://localhost',
  'http://localhost:3000' ]
> urls.map(uri => url.parse(uri).path?url.parse(uri).path.match('^[^?]*')[0].split('/').slice(1)[0] : '' )
[ 'page', 'page', 'page', '', '', '', 'page', 'page', '', '' ]