Skip to content

Commit

Permalink
updated travis
Browse files Browse the repository at this point in the history
  • Loading branch information
syzer committed Oct 14, 2016
1 parent a46099f commit 8f9b52a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo: false
language: node_js
node_js:
- '4'
- '5'
- '6'
60 changes: 60 additions & 0 deletions Trash/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// https://gist.github.com/amejiarosario/53afae82e18db30dadc9bc39035778e5
const http = require('http')
const url = require('url')
const fs = require('fs')
const path = require('path')
const port = process.argv[2] || 9000

http.createServer((req, res) => {
console.log(`${req.method} ${req.url}`)

// parse URL
const parsedUrl = url.parse(req.url)
// extract URL path
let pathname = `.${parsedUrl.pathname}`
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext
// maps file extention to MIME typere
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
}

fs.exists(pathname, (exist) => {
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404
res.end(`File ${pathname} not found!`)
return
}

// if is a directory search for index file matching the extention
if (fs.statSync(pathname).isDirectory()) pathname += '/index' + ext

// read file from file system
fs.readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500
res.end(`Error getting the file: ${err}.`)
} else {
// if the file is found, set Content-type and send data
res.setHeader('Content-type', map[ext] || 'text/plain')
res.end(data)
}
})
})


}).listen(parseInt(port))

console.log(`Server listening on port ${port}`)

0 comments on commit 8f9b52a

Please sign in to comment.