Skip to content

Instantly share code, notes, and snippets.

@reu
Created April 24, 2015 22:06
Show Gist options
  • Select an option

  • Save reu/8b632b0fb22cf006a0f9 to your computer and use it in GitHub Desktop.

Select an option

Save reu/8b632b0fb22cf006a0f9 to your computer and use it in GitHub Desktop.
Node simple static file server.
var http = require("http");
var fs = require("fs");
var mime = require("mime-types");
var server = http.createServer(function(req, res) {
var path = "." + req.url;
fs.stat(path, function(error, stats) {
if (error) {
res.writeHead(404);
res.end();
return
}
res.writeHead(200, {
"Content-Type": mime.lookup(path),
"Content-Length": stats.size,
"Connection": "close"
});
fs.createReadStream(path).pipe(res);
});
});
server.listen(process.env.PORT || 5000);
{
"name": "node",
"version": "1.0.0",
"description": "Simple static file server",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Rodrigo Navarro <[email protected]>",
"license": "MIT",
"dependencies": {
"mime-types": "^2.0.10"
}
}
@erickzanardo
Copy link

Só faltou o charset na resposta =P

"Content-Type": mime.lookup(path) + ";charset=UTF-8"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment