var http = require('http');
var url = require('url');
var fs = require('fs');
var neededstats = [];
http.createServer(function(req, res) {
if (req.url == '/index.html' || req.url == '/') {
fs.readFile('./index.html', function(err, data) {
res.end(data);
});
} else {
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);
});
}
}).listen(8080, '0.0.0.0');
console.log('Server running.');
我没有测试你的代码,但其他的东西工作
如果你想从请求URL
var url_parts = url.parse(req.url);
console.log(url_parts);
console.log(url_parts.pathname);
路径信息1.如果你正在在我的示例中,URL参数仍然无法读取文件,只是纠正了您的文件路径。如果放在同一个目录服务器代码,它会工作的index.html ...
2.如果您有您要使用节点,那么我会建议你使用像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;
来源:http://www.nodebeginner.org/
没错创建一个REST调用看到这篇文章的http://erichonorez.wordpress。 com/2013/02/10/how-create-a-rest-api-with-node-js-and-express/ – lastboy
和浏览器,仅用于快速测试 –