| @@ 1-44 (lines=44) @@ | ||
| 1 | var http = require('http'), |
|
| 2 | url = require('url'), |
|
| 3 | join = require('path').join, |
|
| 4 | extname = require('path').extname, |
|
| 5 | join = require('path').join, |
|
| 6 | fs = require('fs'), |
|
| 7 | port = process.argv[2] || 3000 |
|
| 8 | ||
| 9 | var mime = { |
|
| 10 | 'html': 'text/html', |
|
| 11 | 'css': 'text/css', |
|
| 12 | 'js': 'application/javascript', |
|
| 13 | } |
|
| 14 | ||
| 15 | http.createServer(function(req, res){ |
|
| 16 | console.log(' \033[90m%s \033[36m%s\033[m', req.method, req.url) |
|
| 17 | ||
| 18 | var pathname = url.parse(req.url).pathname, |
|
| 19 | path = join(process.cwd(), pathname) |
|
| 20 | ||
| 21 | function notFound() { |
|
| 22 | res.statusCode = 404 |
|
| 23 | res.end("404 Not Found\n") |
|
| 24 | } |
|
| 25 | ||
| 26 | function error(err) { |
|
| 27 | res.statusCode = 500 |
|
| 28 | res.end(err.message + "\n") |
|
| 29 | } |
|
| 30 | ||
| 31 | fs.exists(path, function(exists){ |
|
| 32 | if (!exists) return notFound() |
|
| 33 | ||
| 34 | fs.stat(path, function(err, stat){ |
|
| 35 | if (err) return error() |
|
| 36 | if (stat.isDirectory()) path = join(path, 'index.html') |
|
| 37 | res.setHeader('Cache-Control', 'no-cache') |
|
| 38 | res.setHeader('Content-Type', mime[path.split('.').slice(-1)] || 'application/octet-stream') |
|
| 39 | fs.createReadStream(path).pipe(res) |
|
| 40 | }) |
|
| 41 | }) |
|
| 42 | }).listen(port) |
|
| 43 | ||
| 44 | console.log('\n Server listening on %d\n', port) |
|
| 45 | ||
| @@ 1-44 (lines=44) @@ | ||
| 1 | var http = require('http'), |
|
| 2 | url = require('url'), |
|
| 3 | join = require('path').join, |
|
| 4 | extname = require('path').extname, |
|
| 5 | join = require('path').join, |
|
| 6 | fs = require('fs'), |
|
| 7 | port = process.argv[2] || 3000 |
|
| 8 | ||
| 9 | var mime = { |
|
| 10 | 'html': 'text/html', |
|
| 11 | 'css': 'text/css', |
|
| 12 | 'js': 'application/javascript', |
|
| 13 | } |
|
| 14 | ||
| 15 | http.createServer(function(req, res){ |
|
| 16 | console.log(' \033[90m%s \033[36m%s\033[m', req.method, req.url) |
|
| 17 | ||
| 18 | var pathname = url.parse(req.url).pathname, |
|
| 19 | path = join(process.cwd(), pathname) |
|
| 20 | ||
| 21 | function notFound() { |
|
| 22 | res.statusCode = 404 |
|
| 23 | res.end("404 Not Found\n") |
|
| 24 | } |
|
| 25 | ||
| 26 | function error(err) { |
|
| 27 | res.statusCode = 500 |
|
| 28 | res.end(err.message + "\n") |
|
| 29 | } |
|
| 30 | ||
| 31 | fs.exists(path, function(exists){ |
|
| 32 | if (!exists) return notFound() |
|
| 33 | ||
| 34 | fs.stat(path, function(err, stat){ |
|
| 35 | if (err) return error() |
|
| 36 | if (stat.isDirectory()) path = join(path, 'index.html') |
|
| 37 | res.setHeader('Cache-Control', 'no-cache') |
|
| 38 | res.setHeader('Content-Type', mime[path.split('.').slice(-1)] || 'application/octet-stream') |
|
| 39 | fs.createReadStream(path).pipe(res) |
|
| 40 | }) |
|
| 41 | }) |
|
| 42 | }).listen(port) |
|
| 43 | ||
| 44 | console.log('\n Server listening on %d\n', port) |
|
| 45 | ||
| @@ 1-44 (lines=44) @@ | ||
| 1 | var http = require('http'), |
|
| 2 | url = require('url'), |
|
| 3 | join = require('path').join, |
|
| 4 | extname = require('path').extname, |
|
| 5 | join = require('path').join, |
|
| 6 | fs = require('fs'), |
|
| 7 | port = process.argv[2] || 3000 |
|
| 8 | ||
| 9 | var mime = { |
|
| 10 | 'html': 'text/html', |
|
| 11 | 'css': 'text/css', |
|
| 12 | 'js': 'application/javascript', |
|
| 13 | } |
|
| 14 | ||
| 15 | http.createServer(function(req, res){ |
|
| 16 | console.log(' \033[90m%s \033[36m%s\033[m', req.method, req.url) |
|
| 17 | ||
| 18 | var pathname = url.parse(req.url).pathname, |
|
| 19 | path = join(process.cwd(), pathname) |
|
| 20 | ||
| 21 | function notFound() { |
|
| 22 | res.statusCode = 404 |
|
| 23 | res.end("404 Not Found\n") |
|
| 24 | } |
|
| 25 | ||
| 26 | function error(err) { |
|
| 27 | res.statusCode = 500 |
|
| 28 | res.end(err.message + "\n") |
|
| 29 | } |
|
| 30 | ||
| 31 | fs.exists(path, function(exists){ |
|
| 32 | if (!exists) return notFound() |
|
| 33 | ||
| 34 | fs.stat(path, function(err, stat){ |
|
| 35 | if (err) return error() |
|
| 36 | if (stat.isDirectory()) path = join(path, 'index.html') |
|
| 37 | res.setHeader('Cache-Control', 'no-cache') |
|
| 38 | res.setHeader('Content-Type', mime[path.split('.').slice(-1)] || 'application/octet-stream') |
|
| 39 | fs.createReadStream(path).pipe(res) |
|
| 40 | }) |
|
| 41 | }) |
|
| 42 | }).listen(port) |
|
| 43 | ||
| 44 | console.log('\n Server listening on %d\n', port) |
|
| 45 | ||