node 实现静态服务server.js

node 实现静态服务server.js

将下面代码保存为server.js,然后放到再需要静态访问的目录下,执行命令:node server.js,
即可通过url 地址访问对应文件。(默认端口:8888)

// 引入相应模块
var http = require('http'),
	 url = require('url'),
	path = require('path'),
	  fs = require('fs');
	  
var port = process.argv[2] || 8888;

var types = {
	'mp3': 'audio/mpeg',
	'html': 'text/html',
	'js': 'application/javascript'
},
site = 'http://localhost:' + port;

http.createServer(function (request, response) {
    var uri = url.parse(request.url).pathname,
    filename = path.join(__dirname, uri);
    
    fs.exists(filename, function (exists) {
       if (!exists) {
           response.writeHead(404, {'Content-Type': 'text/plain', 'X-my-param':'zcyue'});
           response.write('404 Not Found\n');
           response.end();
           return;
       }

       if(!fs.lstatSync(filename).isDirectory()) {
           var type = filename.split('.');
           type = type[type.length - 1];
           response.writeHead(200, { 'Content-Type': types[type] + '; charset=utf-8' });
           fs.createReadStream(filename).pipe(response);
        } else {
           response.writeHead(301, {'Location': site + '/www/app.html' });
           response.end();
        }
    });
}).listen(parseInt(port, 10));

console.log('Static file server running at\n => ' + site + '/\nCTRL + C to shutdown');

至此,结束。

猜你喜欢

转载自blog.csdn.net/Jacoh/article/details/84194685