Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http');
- const port = 8000;
- const url = require('url');
- // const requestHandler = (request, response) => {
- // console.log(request.url);
- // response.end('Hello Node.js Server!');
- // };
- // const requestHandler = (request, response) => {
- // console.log(request.url);
- // if (request.url === '/') {
- // response.end('Hello Node.js Server!');
- // } else if (request.url === '/about') {
- // response.end('This demonstrates routing with Node.js.');
- // } else {
- // response.end('Default page (URLs other than / and /about)');
- // }
- // };
- const requestHandler = (request, response) => {
- // The URL we want to parse (= analyze)
- const exampleUrl = request.url;
- // Check url.parse's doc for the meaning of parameters
- const parsedUrl = url.parse(exampleUrl, true);
- // Show the results
- console.log('Parsed query string:');
- console.log(parsedUrl.query); // { name: 'Jane', city: 'Boston' }
- const {name, city} = parsedUrl.query;
- if (request.url === '/') {
- response.end('Hello Node.js Server!');
- } else if (request.url === '/about') {
- response.end('This demonstrates routing with Node.js.');
- } else if ((name) && (!city)) {
- response.end(`Hello ${name}, from (Please, you do enter a city)`);
- } else if ((city) && (!name)) {
- response.end(`Hello, (Please write your name) from ${city}.`);
- } else if ((name) && (city)) {
- response.end(`Hello, ${name} from ${city}.`);
- } else {
- response.end('Default page (URLs other than / and /about)');
- }
- };
- const server = http.createServer(requestHandler);
- server.listen(port, (err) => {
- if (err) {
- console.error('Something bad happened');
- } else {
- console.log(`server is listening on ${port}`);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement