Last active
March 26, 2020 12:56
-
-
Save lebowvsky/e090a73ffaa8b34db548315f80aa0f99 to your computer and use it in GitHub Desktop.
NodeJS creer 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 urlObj = url.parse(request.url, true).query | |
if(urlObj.name && urlObj.city){ | |
response.end(`Hello ${urlObj.name} How are you in your beautiful city of ${urlObj.city}!!!!`); | |
} else { | |
response.end("Please provide name AND city parameters"); | |
} | |
} | |
const server = http.createServer(requestHandler); | |
server.listen(port, (err) => { | |
if(err){ | |
console.error('It does not work'); | |
} else { | |
console.log(`The server is listening the port : ${port}`) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment