Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gaetan-cordonnier/b58cca953750b91fba14009efcde1470 to your computer and use it in GitHub Desktop.
Save gaetan-cordonnier/b58cca953750b91fba14009efcde1470 to your computer and use it in GitHub Desktop.
Node.js - Créer un serveur HTTP
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