• 静态文件服务器


    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(/
    
    ?
    
    ?/)) {
    24             var line = input.split(/
    /)[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
    ');
    30                 con.write('Content-Type: text/plain
    ');
    31                 con.write('Content-Length: '+body.length+'
    
    ');
    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
    ');
    40                 con.write('Content-Type: text/plain
    ');
    41                 con.write('Content-Length: '+body.length+'
    
    ');
    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
    ');
    51                     con.write('Content-Type: text/plain
    ');
    52                     con.write('Content-Length: '+body.length+'
    
    ');
    53                     con.write(body);
    54                     con.destroy();
    55                 } else {
    56                     con.write('HTTP/1.1 200 OK
    ');
    57                     con.write('Content-Type: '+type+'
    ');
    58                     con.write('Content-Length: '+data.byteLength+'
    
    ');
    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 });
  • 相关阅读:
    Nginx 跨域配置支持
    数据结构与算法分析
    数据结构与算法分析
    数据结构与算法分析
    Bash shell
    Bash shell
    HHUOJ 1040
    HHUOJ 1114
    HDUOJ 1171
    HDUOJ 1428
  • 原文地址:https://www.cnblogs.com/godghdai/p/6917158.html
Copyright © 2020-2023  润新知