Created
June 24, 2016 09:47
-
-
Save LimarenkoDenis/ca98157e2f92e2522b03f5b74f3d91d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"eslint": "^2.11.1", | |
"eslint-config-airbnb-base": "^3.0.1", | |
"eslint-plugin-import": "^1.8.1", | |
"nodemon": "^1.9.2" | |
}, | |
"dependencies": { | |
"fs": "0.0.2", | |
"mime-types": "^2.1.11", | |
"nconf": "^0.8.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const mime = require('mime-types'); | |
http.createServer((request, response) => { | |
let filePath = path.join('public', request.url); | |
if (filePath === path.join('public/')) { | |
filePath = path.join('public', 'index.html'); | |
} | |
const extname = path.extname(filePath); | |
const contentType = mime.contentType(extname); | |
fs.exists(filePath, (exists) => { | |
if (exists) { | |
const readStream = fs.createReadStream(filePath); | |
readStream.pipe(response); | |
readStream.on('end', () => { | |
response.writeHead(200, { 'Content-Type': contentType }); | |
}); | |
} else { | |
fs.readFile(path.join('public', '404.html'), (err, data) => { | |
if (err) throw err; | |
response.writeHead(404, { 'Content-Type': contentType }); | |
response.end(data); | |
}); | |
} | |
}); | |
}).listen(8000); | |
console.log('Server running at http://127.0.0.1:8000/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment