Welcome to the Linux Foundation Forum!

Lab 8.1 - Validation.js issue - returning false negative

Hi @davidmarkclements , Hi all,

I am encountering validation issue when running node validate.js

It returns

PS D :\LinuxFoundation\LFW212\Lab8.1> node .\validate.js
☑️ GET http://localhost:3011/bad-route responded with 404 response
☑️ GET http://localhost:3011/ responded with 400
☑️ GET http://localhost:3011/?url=http://localhost:7686/bad-route responded with 404 response
⛔️ GET http://localhost:3011/?url=http://localhost:7686/redir must forward 301 response from upstream server, got 404

It says, my app doesn't forward 301.

To test this, I created a server that will return me 301 response, running on http://localhost:6000/redir

Then I hit my app with query ?url=http://localhost:6000/redir

It looks fine on Postman

I did the same using chrome browser, and it returns me the same result as Postman (301 response)

I wonder, is there any issue with the validation.js file for Lab 8.1? I am using the newest 2023 version.

My codes looks like this (using Express)

  1. var express = require("express");
  2. var router = express.Router();
  3. var createError = require("http-errors");
  4. const { createProxyMiddleware } = require("http-proxy-middleware");
  5.  
  6. function setProxyTarget(req, res, next) {
  7. const url = req.query.url;
  8.  
  9. if (url) {
  10. try {
  11. new URL(url);
  12. res.locals.url = url;
  13. next();
  14. } catch (err) {
  15. res.status(400);
  16. }
  17. } else {
  18. return next(createError(400));
  19. }
  20. }
  21.  
  22. function proxy(req, res, next) {
  23. const settingProxy = createProxyMiddleware({
  24. target: res.locals.url,
  25. changeOrigin: true,
  26. logger: console,
  27. followRedirects: true,
  28. });
  29.  
  30. settingProxy(req, res, next);
  31. }
  32.  
  33. router.get("/", setProxyTarget, proxy);
  34.  
  35. module.exports = router;
  36.  

Answers

  • Posts: 160
    edited November 2023

    @jimmycheung22 all the code does is hit the end point and check status code response. What might be happening is connection to a server running old code? Are you manually setting the port? 3011 looks human-set to me - if you are then you're probably just hitting the wrong server or something. Testing against another port endpoint doesn't help confirm/deny any issues - you need to check against the same exact server instance

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training