1. 程式人生 > >靜態文件服務器

靜態文件服務器

clas oca tro rip split pan gif sep cal

1 //使用express
2 var express = require(‘express‘);
3 var app = express();
4 app.use(express.static(__dirname + ‘/public‘));
5 app.listen(8080); 
 1 //使用connect
 2 var path = require(‘path‘);
 3 var connect = require(‘connect‘);
 4 var app = connect();
 5 var fs = require(‘fs‘);
 6 
 7 var dir = path.join(__dirname, ‘public‘);
8 9 var mime = { 10 html: ‘text/html‘, 11 txt: ‘text/plain‘, 12 css: ‘text/css‘, 13 gif: ‘image/gif‘, 14 jpg: ‘image/jpeg‘, 15 png: ‘image/png‘, 16 svg: ‘image/svg+xml‘, 17 js: ‘application/javascript‘ 18 }; 19 20 app.use(function (req, res) { 21 var reqpath = req.url.toString().split(‘?‘)[0];
22 if (req.method !== ‘GET‘) { 23 res.statusCode = 501; 24 res.setHeader(‘Content-Type‘, ‘text/plain‘); 25 return res.end(‘Method not implemented‘); 26 } 27 var file = path.join(dir, reqpath.replace(/\/$/, ‘/index.html‘)); 28 if (file.indexOf(dir + path.sep) !== 0) {
29 res.statusCode = 403; 30 res.setHeader(‘Content-Type‘, ‘text/plain‘); 31 return res.end(‘Forbidden‘); 32 } 33 var type = mime[path.extname(file).slice(1)] || ‘text/plain‘; 34 var s = fs.createReadStream(file); 35 s.on(‘open‘, function () { 36 res.setHeader(‘Content-Type‘, type); 37 s.pipe(res); 38 }); 39 s.on(‘error‘, function () { 40 res.setHeader(‘Content-Type‘, ‘text/plain‘); 41 res.statusCode = 404; 42 res.end(‘Not found‘); 43 }); 44 }); 45 46 app.listen(3000, function () { 47 console.log(‘Listening on http://localhost:3000/‘); 48 });
 1 //http
 2 var path = require(‘path‘);
 3 var http = require(‘http‘);
 4 var fs = require(‘fs‘);
 5 
 6 var dir = path.join(__dirname, ‘public‘);
 7 
 8 var mime = {
 9     html: ‘text/html‘,
10     txt: ‘text/plain‘,
11     css: ‘text/css‘,
12     gif: ‘image/gif‘,
13     jpg: ‘image/jpeg‘,
14     png: ‘image/png‘,
15     svg: ‘image/svg+xml‘,
16     js: ‘application/javascript‘
17 };
18 
19 var server = http.createServer(function (req, res) {
20     var reqpath = req.url.toString().split(‘?‘)[0];
21     if (req.method !== ‘GET‘) {
22         res.statusCode = 501;
23         res.setHeader(‘Content-Type‘, ‘text/plain‘);
24         return res.end(‘Method not implemented‘);
25     }
26     var file = path.join(dir, reqpath.replace(/\/$/, ‘/index.html‘));
27     if (file.indexOf(dir + path.sep) !== 0) {
28         res.statusCode = 403;
29         res.setHeader(‘Content-Type‘, ‘text/plain‘);
30         return res.end(‘Forbidden‘);
31     }
32     var type = mime[path.extname(file).slice(1)] || ‘text/plain‘;
33     var s = fs.createReadStream(file);
34     s.on(‘open‘, function () {
35         res.setHeader(‘Content-Type‘, type);
36         s.pipe(res);
37     });
38     s.on(‘error‘, function () {
39         res.setHeader(‘Content-Type‘, ‘text/plain‘);
40         res.statusCode = 404;
41         res.end(‘Not found‘);
42     });
43 });
44 
45 server.listen(3000, function () {
46     console.log(‘Listening on http://localhost:3000/‘);
47 });
 1 //net
 2 var path = require(‘path‘);
 3 var net = require(‘net‘);
 4 var fs = require(‘fs‘);
 5 
 6 var dir = path.join(__dirname, ‘public‘);
 7 
 8 var mime = {
 9     html: ‘text/html‘,
10     txt: ‘text/plain‘,
11     css: ‘text/css‘,
12     gif: ‘image/gif‘,
13     jpg: ‘image/jpeg‘,
14     png: ‘image/png‘,
15     svg: ‘image/svg+xml‘,
16     js: ‘application/javascript‘
17 };
18 
19 var server = net.createServer(function (con) {
20     var input = ‘‘;
21     con.on(‘data‘, function (data) {
22         input += data;
23         if (input.match(/\n\r?\n\r?/)) {
24             var line = input.split(/\n/)[0].split(‘ ‘);
25             var method = line[0], url = line[1], pro = line[2];
26             var reqpath = url.toString().split(‘?‘)[0];
27             if (method !== ‘GET‘) {
28                 var body = ‘Method not implemented‘;
29                 con.write(‘HTTP/1.1 501 Not Implemented\n‘);
30                 con.write(‘Content-Type: text/plain\n‘);
31                 con.write(‘Content-Length: ‘+body.length+‘\n\n‘);
32                 con.write(body);
33                 con.destroy();
34                 return;
35             }
36             var file = path.join(dir, reqpath.replace(/\/$/, ‘/index.html‘));
37             if (file.indexOf(dir + path.sep) !== 0) {
38                 var body = ‘Forbidden‘;
39                 con.write(‘HTTP/1.1 403 Forbidden\n‘);
40                 con.write(‘Content-Type: text/plain\n‘);
41                 con.write(‘Content-Length: ‘+body.length+‘\n\n‘);
42                 con.write(body);
43                 con.destroy();
44                 return;
45             }
46             var type = mime[path.extname(file).slice(1)] || ‘text/plain‘;
47             var s = fs.readFile(file, function (err, data) {
48                 if (err) {
49                     var body = ‘Not Found‘;
50                     con.write(‘HTTP/1.1 404 Not Found\n‘);
51                     con.write(‘Content-Type: text/plain\n‘);
52                     con.write(‘Content-Length: ‘+body.length+‘\n\n‘);
53                     con.write(body);
54                     con.destroy();
55                 } else {
56                     con.write(‘HTTP/1.1 200 OK\n‘);
57                     con.write(‘Content-Type: ‘+type+‘\n‘);
58                     con.write(‘Content-Length: ‘+data.byteLength+‘\n\n‘);
59                     con.write(data);
60                     con.destroy();
61                 }
62             });
63         }
64     });
65 });
66 
67 server.listen(3000, function () {
68     console.log(‘Listening on http://localhost:3000/‘);
69 });

靜態文件服務器