'use strict'
var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path');
var server = http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
var filepath = path.join('.', pathname);
fs.stat(filepath, function(err, stats) {
if (!err && stats.isFile()) {
response.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream(filepath).pipe(response);
} else if (!err && stats.isDirectory()) {
fs.readdir(filepath, function(err, files) {
if (err) {
console.log(err);
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('<p>File list:</p>');
response.write('<ul>');
files.forEach(function(file) {
response.write('<li>' + file + '</li>')
});
response.end('</ul>');
}
});
} else {
response.writeHead(404);
response.end('404 Not Found');
}
});
console.log(filepath);
});
server.listen(8000);
console.log('listen on 8000');
访问文件目录,文件
最新推荐文章于 2022-04-01 19:20:29 发布