Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File 02_UrlParameters/app.js
- const express = require("express");
- const app = express();
- function printReqSummary(request) {
- console.log(`Handling ${request.method} ${request.originalUrl}`);
- }
- // GET / -- Show main page
- app.get("/", function(request, response) {
- printReqSummary(request);
- response.send(
- `<h1>URL Parameters (and Queries)</h1><ul>
- <li>Show normal message (GET /hello/segment1)</li>
- <li>Show special message (GET /hello/segment1/segment2?age=NUMBER&height=NUMBER)</li>
- <li> where segment1, segment2 - any valid URL part</li>
- </ul>`
- );
- });
- // GET /hello/:name -- Show normal message for a named person
- app.get("/hello/:name", function(request, response) {
- printReqSummary(request);
- // Grab URL parameters from `request.params` object
- response.send(`<p>Normal message for: ${request.params.name}</p>`);
- });
- // GET /hello/:name/:surname -- Show special message with plenty of parameters
- app.get("/hello/:name/:surname", function(request, response) {
- printReqSummary(request);
- // Grab (optional) URL queries from `request.query` object
- const age = request.query.age !== undefined ? request.query.age : "unknown";
- const height =
- request.query.height !== undefined ? request.query.height : "unknown";
- response.send(
- `<p>Special message for: ${request.params.name} ${request.params.surname}
- (age: ${age} years, height: ${height} cm)</p>`
- );
- });
- const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
- app.get("/rand/:name/:surname/", (request, response) => {
- printReqSummary(request);
- const params = [];
- params[0] = [request.query.age !== undefined ? request.query.age : "unknown", "age"];
- params[1] = [request.query.height !== undefined ? request.query.height : "unknown", "height"];
- params[2] = [request.query.weight !== undefined ? request.query.weight : "unknown", "weight"];
- const rand = getRandomInt(0, 2);
- response.send(`<p>Rand parameter: ${params[rand][1]}=${params[rand][0]}</p>`);
- });
- app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement