Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created March 21, 2019 13:18
Show Gist options
  • Save johnnyferreiradev/ef2fca56af97e036e0c5a5839facf071 to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/ef2fca56af97e036e0c5a5839facf071 to your computer and use it in GitHub Desktop.
Servidor de rotas com leitura e escrita de arquivos html usando http, url e fs (NodeJs)
const http = require('http')
const url = require('url')
const fs = require('fs')
const server = http.createServer(function(req, res){
var result = url.parse(req.url, true)
const lerArquivo = (caminho) => {
fs.readFile(__dirname + caminho, function(err, html){
res.writeHeader(200, {"Content-Type": "text/html"})
res.end(html)
})
}
if(result.pathname == "/"){
lerArquivo("/artigos.html")
}else if(result.pathname == "/artigos.html"){
lerArquivo(result.pathname)
}else if(result.pathname == "/contato.html"){
lerArquivo(result.pathname)
}else{
lerArquivo("/erro.html")
}
})
server.listen(3000, function(){
console.log("Servido rodando na porta 3000...");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment