Skip to content

Instantly share code, notes, and snippets.

@Rooarii
Last active November 30, 2020 15:22
Show Gist options
  • Save Rooarii/b92cfa8f7945aff956c8c75d932dc75d to your computer and use it in GitHub Desktop.
Save Rooarii/b92cfa8f7945aff956c8c75d932dc75d to your computer and use it in GitHub Desktop.
Node.js - Créer un serveur HTTP
Node.js - Créer un serveur HTTP
// HTTP server type require http
const http = require('http');
// port on which the server will run
const port = 8000;
// url module import
const url = require('url');
// request is a method that will send a response to the client for each request received from client.
const requestHandler = (request, response) => {
console.log(request.url);
const parsedUrl = url.parse((request.url), true);
console.log(parsedUrl.query);
const {name, city }= parsedUrl.query;
if(!name&&!city){
response.end('Please provide name and city parameters');
}else if (!name||!city){
response.end(`Please provide ${(!name&&'name')||(!city&&'city')} parameter`);
console.log((!name&&'name')||(!city&&'city'))
} else {
response.end(`Hello ${name} from ${city}!`)
}
};
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
console.error('Something 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