Created
March 21, 2019 13:18
-
-
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)
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 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