美文网首页
创建一个简单的server

创建一个简单的server

作者: zz张哲 | 来源:发表于2016-10-06 15:26 被阅读0次
    var http = require('http');
    var url = require('url');
    var fs = require('fs');
    var mime = {
        html: 'text/html',
        jpg: 'image/jpeg',
        png: 'image/png',
        css: 'text/css'
    };
    
    var server = http.createServer(function (request, response) {
        if (request.url != '/favicon.ico') {
            var pathname = url.parse(request.url).pathname;
            fs.readFile('.' + pathname, 'binary', function (err, data) {
                if (err) throw err;
                else {
                    response.writeHead(200, {'Content-Type': mime[pathname.split('.')[1]]});
                    response.write(data, 'binary');
                    response.end('');
                }
            });
        }
    });
    
    server.listen(8000);
    console.log('server listen on port 8000');

    相关文章

      网友评论

          本文标题:创建一个简单的server

          本文链接:https://www.haomeiwen.com/subject/yrbeyttx.html