Created
November 30, 2020 12:29
-
-
Save gaetan-cordonnier/b58cca953750b91fba14009efcde1470 to your computer and use it in GitHub Desktop.
Node.js - Créer un serveur HTTP
This file contains hidden or 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 port = 8000; | |
const requestHandler = (request, response) => { | |
const urlValue = request.url; | |
const parseUrl = url.parse(urlValue, true); | |
const nameValue = parseUrl.query; | |
if (request.url === "/") { | |
response.end("Please provide name and city parameters"); | |
} else { | |
response.end(`Hello, ${nameValue.name} from ${nameValue.city}!`); | |
} | |
}; | |
const server = http.createServer(requestHandler); | |
server.listen(port, (err) => { | |
if (err) { | |
console.error("Someting bad happened"); | |
} else { | |
console.log(`Server is listening on ${port}`); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment